我有一个存储过程接受一个日期输入,如果没有传入值,则该日期输入稍后设置为当前日期:
CREATE PROCEDURE MyProc
@MyDate DATETIME = NULL
AS
IF @MyDate IS NULL SET @MyDate = CURRENT_TIMESTAMP
-- Do Something using @MyDate
Run Code Online (Sandbox Code Playgroud)
我遇到问题,如果@MyDate在NULL第一次编译存储过程时传入if ,则对于所有输入值(NULL或其他),性能总是很糟糕,如果在编译存储过程时传入日期/当前日期性能适用于所有输入值(NULL或其他).
令人困惑的是,即使实际 使用@MyDate的值NULL(而不是CURRENT_TIMESTAMP由IF语句设置),生成的糟糕执行计划也很糟糕
我发现禁用参数嗅探(通过欺骗参数)修复了我的问题:
CREATE PROCEDURE MyProc
@MyDate DATETIME = NULL
AS
DECLARE @MyDate_Copy DATETIME
SET @MyDate_Copy = @MyDate
IF @MyDate_Copy IS NULL SET @MyDate_Copy = CURRENT_TIMESTAMP
-- Do Something using @MyDate_Copy
Run Code Online (Sandbox Code Playgroud)
我知道这与参数嗅探有关,但我看到的所有"参数嗅探变坏"的例子都涉及使用传递的非代表性参数编译存储过程,但是在这里我看到了执行计划是可怕的SQL Server可能认为执行该语句其中参数可能需要在该点所有可能的值- NULL,CURRENT_TIMESTAMP或以其他方式.
有没有人知道为什么会这样?
是否有可能以编程方式获取LINQ to SQL或ADO.NET Query的执行计划以显示调试信息?如果是这样,怎么样?
所以我(仍)经历了一些缓慢的遗留sql视图,用于计算(有时)大量数据集的某些平均值和标准偏差.我最终得到的是加入观点等观点的观点.
所以我虽然会审查我的查询的执行计划.它立即提出了一个缺失的索引,然后我实现了.但它仍然无法忍受缓慢(这么慢,它会超出VB6应用查询数据的速度;))
因此,在进一步研究执行计划后,我发现成本最高(在我的情况下每个约8%)是"Paralellism"案例.主要是"分发流"和"重新分配流".这些是什么?
对于Oracle 11.2.0.2.0中对大量数据进行中型查询的查询执行计划,我遇到了一些问题.为了加快速度,我引入了一个范围过滤器,其大致类似于:
PROCEDURE DO_STUFF(
org_from VARCHAR2 := NULL,
org_to VARCHAR2 := NULL)
-- [...]
JOIN organisations org
ON (cust.org_id = org.id
AND ((org_from IS NULL) OR (org_from <= org.no))
AND ((org_to IS NULL) OR (org_to >= org.no)))
-- [...]
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我想限制JOIN的organisations使用数量组织的可选范围.客户端代码可以调用DO_STUFF(假设是快速)或没有(非常慢)限制.
问题是,PL/SQL将为上面org_from和org_to参数创建绑定变量,这在大多数情况下是我所期望的:
-- [...]
JOIN organisations org
ON (cust.org_id = org.id
AND ((:B1 IS NULL) OR (:B1 <= org.no))
AND ((:B2 IS NULL) OR (:B2 >= org.no)))
-- [...] …Run Code Online (Sandbox Code Playgroud) SQL 2008.
我有一个测试表:
create table Sale
(
SaleId int identity(1, 1)
constraint PK_Sale primary key,
Test1 varchar(10) null,
RowVersion rowversion not null
constraint UQ_Sale_RowVersion unique
)
Run Code Online (Sandbox Code Playgroud)
我用10k测试行填充它.
declare @RowCount int = 10000
while(@RowCount > 0)
begin
insert Sale default values
set @RowCount -= 1
end
Run Code Online (Sandbox Code Playgroud)
我运行这两个查询:
-- Query #1
select *
from Sale
where RowVersion > 0x000000000001C310
-- Query #2
declare @LastVersion rowversion = 0x000000000001C310
select *
from Sale
where RowVersion > @LastVersion
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚为什么这两个查询有不同的执行计划.
查询#1对UQ_Sale_RowVersion索引执行索引查找.
查询#2对PK_Sale进行索引扫描.
我想查询#2做索引搜索.
我将不胜感激.
谢谢.
[编辑] …
sql sql-server query-optimization sql-server-2008 sql-execution-plan
有没有办法只显示大于批次1%的项目?
我试图在proc中找到瓶颈,它包含循环和其他逻辑以及我不关心的99%的最终执行计划,但很难在Management Studio中滚动并实际找到减慢它的部分.
EXPLAIN QUERY PLAN在SQLite 3中使用它有时会给我输出如
SEARCH TABLE staff AS s USING AUTOMATIC COVERING INDEX (is_freelancer=? AND sap=?) (~6 rows)
Run Code Online (Sandbox Code Playgroud)
索引来自何处以及它的作用是什么?该表没有手动创建的索引.
我在一个简单的SELECT语句中使用了Oracle数据库中的EXPLAIN PLAN,以了解它是如何工作的.在EXPLAIN PLAN的一个输出中提到了,table access by index rowid而在另一个输出中table access by index rowid BATCHED.他们之间有什么区别?
我遇到了一些奇怪的东西,我无法解释.
我正在使用以下查询:
MERGE INTO Main_Table t
USING Stg_Table s
ON(s.site_id = t.site_id)
WHEN MATCHED THEN
UPDATE SET t.arpu_prev_period = s.arpu_prev_period
.... --50 more columns
where t.period_code = 201612
Run Code Online (Sandbox Code Playgroud)
Stg_Table:索引(Site_Id)
Main_Table:
- 索引(Period_code,Site_id)
- 由period_code分区
- 注 - 我尝试在Site_Id单独的,相同的执行计划上添加索引.
我期望一个使用单分区扫描的执行计划,但我得到了Partition list all.
这是执行计划:
6 | 0 | MERGE STATEMENT | |
7 | 1 | MERGE | Main_Table |
8 | 2 | VIEW | |
9 | 3 | HASH JOIN | |
10 | 4 …Run Code Online (Sandbox Code Playgroud) 我想在Postgres中利用仅索引扫描的强大功能,并尝试使用一个表:
CREATE TABLE dest.contexts
(
id integer NOT NULL,
phrase_id integer NOT NULL,
lang character varying(5) NOT NULL,
ranking_value double precision,
index_min integer,
index_max integer,
case_sensitive boolean,
is_enabled boolean,
is_to_sync boolean NOT NULL DEFAULT true
);
insert into dest.contexts select * from source.contexts;
alter table dest.contexts
add constraint pk_contexts primary key (id, phrase_id, lang);
CREATE INDEX idx_contexts_
ON dest.contexts
USING btree
(id, is_enabled, lang, phrase_id, ranking_value, index_min, index_max, case_sensitive);
Run Code Online (Sandbox Code Playgroud)
索引涵盖了我想在下一个查询中使用的所有列:
explain analyze
select ranking_value, index_min, index_max, case_sensitive
from dest.contexts
where …Run Code Online (Sandbox Code Playgroud) postgresql vacuum sql-execution-plan postgresql-9.4 autovacuum
sql ×4
sql-server ×4
oracle ×3
.net ×1
autovacuum ×1
c# ×1
linq-to-sql ×1
performance ×1
plsql ×1
postgresql ×1
sqlite ×1
t-sql ×1
vacuum ×1