我有一个动态创建的 LINQ to SQL 查询。有趣的是,当我在 SQL Management Studio 中运行它时,速度非常快。当我从 L2S 运行它时,它会在一段时间后变得非常慢。
这可能是因为查询计划/执行计划。当我重新启动 SQL Server 时,L2S 查询也再次闪电般快速。
现在使用 T-SQL,您可以使用 WITH RECOMPILE。但是如何用 L2S 做到这一点呢?
如果您有一个低效的查询,并且您添加了一个索引来帮助提高性能,那么查询是否应该“立即”开始使用该索引?
或者您是否需要通过运行 来清除 Oracle“缓存”(我相信是 v$sql)alter system flush shared_pool;
?
Why is the elapsed execution time of first of the two queries below lower, while the estimated subtree cost is higher?
Estimated subtree cost is only a guideline for gauging query plan performance in conjunction with other query performance indicators, like response time, but I'm surprised by the existance of an inverse relationship between cost and time when comparing two similar queries side by side.
I've created an example database to illustrate this problem and included the resulting …
我有这样的简单查询:
SELECT *
FROM t1
WHERE f1 > 42
AND f2 = 'foo'
AND f3 = 'bar'
ORDER BY f4 DESC
LIMIT 10 OFFSET 100;
Run Code Online (Sandbox Code Playgroud)
我有字段f4的索引(用于其他查询)。条件“f1 > 42 AND f2 = 'foo' AND f3 = 'bar'”不具有代表性,对应于表 t1 中 70% 的记录。表中约有 2 000 000 条记录,并且每天都在增长。该查询的查询计划解释显示了对整个表使用 seq 扫描,然后执行排序和限制。
Postgres 是否可以这样执行此查询:
在本次查询的查询计划中,对 grp_set 的计算重复了 4 次(distinct sort 每次占用 23%,所以占用了所有资源的 23 * 4 = 92%):
with
grp_set as (select distinct old_num,old_tbl,old_db,old_val_num from err_calc)
,grp as (select id = row_number() over (order by old_num),* from grp_set)
,leaf as (select grp.id ,c.* ,sort = convert(varchar(max),old_col) + " - " + severity + " - " + err
from grp
join err_calc c on
c.old_num = grp.old_num
and c.old_tbl = grp.old_tbl
and c.old_db = grp.old_db
and c.old_val_num = grp.old_val_num
)
select old_num,old_tbl,old_db,old_val_num,conc.*
from (select sep=",") …
Run Code Online (Sandbox Code Playgroud) sql sql-server performance common-table-expression sql-execution-plan
该数据库是示例 Oracle HR 数据库:http : //elsasoft.com/samples/oracle/Oracle.XE.HR/default.htm
解释计划:
----------------------------------------------------------
| Id | Operation | Name |
----------------------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | HASH UNIQUE | |
|* 2 | TABLE ACCESS BY INDEX ROWID | EMPLOYEES |
|* 3 | INDEX RANGE SCAN | EMP_DEPARTMENT_IX |
----------------------------------------------------------
Predictate Information (identified by operation id):
----------------------------------------------------
2 - filter("MANAGER_ID" < 150)
3 - access("DEPARTMENT_ID" < 50)
Run Code Online (Sandbox Code Playgroud)
我试过这个查询,但它产生了非常不同的结果:
select /*+ use_hash(emp) */*
from HR.employees emp
where MANAGER_ID <150 …
Run Code Online (Sandbox Code Playgroud) 我有一个选择查询,我需要包含以下列(几乎表中的所有列):
SELECT ANI,CALL_ID, DATE_TIME, ABANDON_RATE, CALL_TYPE, CAMPAIGN, CAMPAIGN_TYPE, CUSTOMER_NAME, DISPOSITION, DNIS, LIST_NAME,
SESSION_ID, SKILL, AGENT_EMAIL, AGENT_GROUP, THIRD_PARTY_TALK_TIME, AFTER_CALL_WORK_TIME, HOLD_TIME, CALL_TIME, HANDLE_TIME, IVR_TIME,
MANUAL_TIME, TALK_TIME, QUEUE_WAIT_TIME, advertiserName, affiliateName, callerID, campaignName, ChargentSFA__Biling_Phone__c, CONTACT_CREATE_TIMESTAMP,
CONTACT_ID, CONTACT_MODIFIED_TIMESTAMP, Date_Added, destinationPhoneNumber, leadsource, number1, salesforce_id, source, transactionID,
IVR_PATH
FROM call_data_report
WHERE ANI= '123456' OR DNIS = '123456'
ORDER BY DATE_TIME desc;
Run Code Online (Sandbox Code Playgroud)
此查询执行时间过长(超过 3 分钟)。为了提高查询性能,我在 where 子句中使用的列(即 ANI 和 DNIS)上创建了索引。
create index ani_idx on call_data_report(ANI,DNIS)
Run Code Online (Sandbox Code Playgroud)
但是,当我检查上述选择查询的 EXPLAIN 的输出时,它显示它没有使用索引。是因为所有这些专栏吗?我需要创建什么类型的索引来提高上述查询的性能。
注意:ANI 和 DNIS 的列中有 NULL 值。
谢谢!
在使用 python 和psycopg2
模块时,有什么方法可以打印 SQL 执行计划中的信息以在我的终端中查看它吗?
我尝试了以下操作,但终端中没有显示任何内容:
cur.execute(cur.mogrify('explain analyze ' + sql_query, vals)
Run Code Online (Sandbox Code Playgroud)
并使用print
回报None
:
print(cur.execute(cur.mogrify('explain analyze ' + sql_query, vals))
Run Code Online (Sandbox Code Playgroud)
从这个问题,我也尝试了以下,但也没有奏效:
cur.execute("LOAD 'auto_explain';")
cur.execute("SET auto_explain.log_min_duration = {min_ms};".format(min_ms=0))
cur.execute(sql_query, vals)
Run Code Online (Sandbox Code Playgroud) 在 SQL Developer 中使用解释计划选项时,出现以下错误(在屏幕截图中提到)。
会话中没有未提交的事务。
即使第一次打开 SQL Developer,我也会收到此错误。
这是我的解释计划:
SELECT STATEMENT, GOAL = ALL_ROWS 244492 4525870 235345240
SORT ORDER BY 244492 4525870 235345240
**PARTITION RANGE ALL** 207633 4525870 235345240
INDEX FAST FULL SCAN MCT MCT_PLANNED_CT_PK 207633 4525870 235345240
Run Code Online (Sandbox Code Playgroud)
只是想知道这是否是查询大型分区表的最佳优化计划.
使用Oracle10g
indexing ×3
oracle ×3
sql-server ×3
explain ×2
sql ×2
linq-to-sql ×1
mysql ×1
optimization ×1
oracle10g ×1
performance ×1
postgresql ×1
psycopg2 ×1
python-3.x ×1