根据user_id我在子句中使用的查询,我遇到了性能问题WHERE。
这个问题描述了一个非常相似的问题,但不完全相同:
这是我的查询:
select inlineview.user_search_id, inlineview.user_id,
to_char(timezone('UTC', to_timestamp(date_part('epoch',
inlineview.last_access_date))),'YYYY-MM-DD"T"HH24:MI:SS.MSZ')
last_access_date,
to_char(timezone('UTC', to_timestamp(date_part('epoch',
inlineview.sys_creation_date))),'YYYY-MM-DD"T"HH24:MI:SS.MSZ')
sys_creation_date, b.product_id,
to_char(timezone('UTC', to_timestamp(date_part('epoch',
b.last_prov_change))),'YYYY-MM-DD"T"HH24:MI:SS.MSZ')
last_prov_change,b.product_type,b.tlc_id,b.prod_history_id,
coalesce(d.address_id,-1) address_id,coalesce(d.locality_name,'')
locality_name,
coalesce(d.post_code_zone,'') post_code_zone,
coalesce(d.road_name_concat,'')
road_name_concat,coalesce(d.street_num_concat,'') street_num_concat,
coalesce(d.address_name,'') address_name,
coalesce(d.town_name,'') town_name
from
(select a.user_search_id,
a.user_id,a.last_access_date,
a.sys_creation_date,a.product_id
from linetest.user_search a
where a.user_id = '818901'
order by a.last_access_date desc limit 10) inlineview,
linetest.prod_history b left outer join linetest.address d on d.tlc_id = b.tlc_id
where b.product_id = inlineview.product_id
and b.prod_history_id = (select c.prod_history_id
from linetest.prod_history c
where …Run Code Online (Sandbox Code Playgroud) postgresql performance index execution-plan postgresql-performance
我正在尝试创建一个查询执行计划的示例,它表明如果我对表的某些列进行基数估计,我可以决定哪个查询执行计划的成本最低。
所以我有这两篇文章(1和2),我可以将估计基数理解为列上不同值的数量。然后我计算选择性。
SELECT max(price) FROM tickets WHERE country = "CANADA";
Run Code Online (Sandbox Code Playgroud)
如果我的桌子有 180 件物品,而只有 10 件是加拿大的。而且只有 4 个不同的国家(加拿大、巴西、美国、德国)。所以....
列 country é 4的基数,因为它是不同项目的数量。
列 country = CANADA的选择性是访问的项目数除以表中的项目数。10/180 = 0.0555。
但是优化器可以根据基数选择哪些不同的查询执行计划?
如果这不是一个很好的例子,有人可以指出一个查询,基数对于优化器来说是一种财富,以便决定选择一个计划还是另一个计划?
谢谢,费利佩
sql-server optimization execution-plan cardinality-estimates
我已经计算出所有隐式转换,但我仍然在计划中看到它的提及。我已附上该计划,任何建议都会有所帮助。
select cardholder_index, sum(value) as [RxCost]
into #rxCosts
from RiskPredictionStatistics with (nolock) where model_name = 'prescription_cost_12_months'
and model_set_name = 'rx_updated' and run_id in (select value from #runIds)
and exists (select 1 from StringContainsHelper with (nolock) where IntValue = cardholder_index and ReferenceId = @stringContainsHelperRefId)
group by cardholder_index
Run Code Online (Sandbox Code Playgroud)
我想知道是否有任何方法可以重写查询中的表达式(只是表达式,而不是整个查询)以短路 THEN 阶段的无用评估?
演示数据:
CREATE TABLE #Docs (
ID INT NOT NULL
,DocType TINYINT NOT NULL
);
CREATE TABLE #DocsItems (
IDDocs INT NOT NULL
,Amount NUMERIC(19,6)
);
INSERT INTO #Docs(ID, DocType) VALUES(1,1),(2,1),(3,2),(4,2),(5,2),(6,2);
INSERT INTO #DocsItems(IDDocs,Amount) VALUES(3,50.),(3,25.),(3,33.),(4,44.),(4,123.),(6,11.);
Run Code Online (Sandbox Code Playgroud)
主题查询:
SELECT
-- expression
SumAmount = CASE
WHEN D.DocType <> 1 THEN (SELECT SUM(Amount) FROM #DocsItems WHERE IDDocs = D.ID)
END
FROM #Docs D
WHERE D.DocType = 1 -- so CASE condition evaluates to False
Run Code Online (Sandbox Code Playgroud)
如果我将查询(故意)重写为:
SELECT
-- expression
SumAmount = CASE …Run Code Online (Sandbox Code Playgroud) 在SQL Server中,我们如何在查询执行后查看其实际执行计划?
据我所知,只有估计的执行计划(而不是实际的执行计划)存储在计划缓存中。那是对的吗?
注意:我们没有任何第三方监控工具。
我有以下(愚蠢地简化的)查询,它利用两个引用同一个表的 CTE 并将它们相互连接:
WITH CTE1 AS
(
SELECT dbo.RemoveNonNumericCharacters(PhoneNumber) AS PhoneNumberCleaned
FROM PhoneNumbersTable
GROUP BY dbo.RemoveNonNumericCharacters(PhoneNumber)
),
CTE2 AS
(
SELECT CTE1.PhoneNumberCleaned
FROM CTE1
INNER HASH JOIN PhoneNumbersTable
ON CTE1.PhoneNumbersCleaned = dbo.RemoveNonNumericCharacters(PhoneNumbersTable.PhoneNumber)
WHERE PhoneNumbersTable.AreaCode IN (718, 212)
)
SELECT PhoneNumberCleaned
FROM CTE2
Run Code Online (Sandbox Code Playgroud)
注意HASH JOIN里面发生的事情CTE2。到目前为止,这一切都运作良好。
如果我将以下WHERE子句添加到最终SELECT查询中,那么我的整个查询现在变为:
WITH CTE1 AS
(
SELECT dbo.RemoveNonNumericCharacters(PhoneNumber) AS PhoneNumberCleaned
FROM PhoneNumbersTable
GROUP BY dbo.RemoveNonNumericCharacters(PhoneNumber)
),
CTE2 AS
(
SELECT CTE1.PhoneNumberCleaned
FROM CTE1
INNER HASH JOIN PhoneNumbersTable
ON CTE1.PhoneNumbersCleaned …Run Code Online (Sandbox Code Playgroud) 我有以下查询:
select ro.*
from courier c1
join courier c2 on c2.real_physical_courier_1c_id = c1.real_physical_courier_1c_id
join restaurant_order ro on ro.courier_id = c2.id
left join jsonb_array_elements(items) jae on true
left join jsonb_array_elements(jae->'options') ji on true
inner join catalogue c on c.id in ((jae->'id')::int, (ji->'id')::int)
join restaurant r on r.id = ro.restaurant_id
where c1.id = '7b35cdab-b423-472a-bde1-d6699f6cefd3' and ro.status in (70, 73)
group by ro.order_id, r.id ;
Run Code Online (Sandbox Code Playgroud)
以下是占用约 95% 时间的查询计划的一部分:
-> Parallel Bitmap Heap Scan on restaurant_order ro (cost=23.87..2357.58 rows=1244 width=1257) (actual time=11.931..38.163 rows=98 loops=2)" …Run Code Online (Sandbox Code Playgroud) 我在 SQL Server 中遇到一个问题,其中非聚集索引查找的性能很差。
下面是实际的执行计划 https://www.brentozar.com/pastetheplan/?id=Sk3-4JGAK
我怎样才能提高绩效?
下面是表定义
CREATE TABLE [Parts].[ManufacturingData](
[LeadFinishId] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[PartID] [int] NOT NULL,
[LeadFinishMaterial] [varchar](50) NULL,
[CreatedDate] [datetime] NULL,
[CreatedBy] [int] NULL,
[ModifiedDate] [datetime] NULL,
[Modifiedby] [int] NULL,
[DeletedDate] [datetime] NULL,
[DeletedBy] [int] NULL,
[Revision_Id] [int] NULL,
[BaseMaterialID] [int] NULL,
[MSLID] [int] NULL,
[MSLSource_Revision_id] [int] NULL,
[MaximumReflowTemperatureID] [int] NULL,
[ReflowTemperatureSource_Revision_Id] [int] NULL,
[MaximumWaveTemperatureID] [int] NULL,
[WaveTemperatureSource_Revision_ID] [int] NULL,
[ReflowSolderTimeID] [int] NULL,
[WaveSolderTimeID] [int] NULL,
[NumberOfReflowCycleID] [int] NULL,
[LeadFinishPlatingID] [int] NULL, …Run Code Online (Sandbox Code Playgroud) sql-server constraint stored-procedures execution-plan sql-server-2016
我正在放置一个查询来列出当前正在执行的请求中存在的关键查找,我基本上想解决这个问题,看看是否可以从执行计划中消除这些关键查找。
为了获取这些键查找,我使用以下查询:
SELECT
er.session_id,
er.blocking_session_id,
er.start_time,
er.status,
dbName = DB_NAME(er.database_id),
er.wait_type,
er.wait_time,
er.last_wait_type,
er.granted_query_memory,
er.reads,
er.logical_reads,
er.writes,
er.row_count,
er.total_elapsed_time,
er.cpu_time,
er.open_transaction_count,
er.open_transaction_count,
s.text,
qp.query_plan,
logDate = CONVERT(DATETIME,GETDATE()),
logTime = CONVERT(DATETIME,GETDATE())
FROM sys.dm_exec_requests er
CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) s
CROSS APPLY sys.dm_exec_query_plan(er.plan_handle) qp
WHERe er.session_id <> @@SPID
and CONVERT(VARCHAR(MAX), qp.query_plan) LIKE '%IndexScan Lookup%'
Run Code Online (Sandbox Code Playgroud)
我在这个查询中面临的问题是它返回任何,无论其成本key lookup如何。
我想过滤那些,我只想看到昂贵的关键查找。
如何过滤查询以仅显示昂贵的查找操作?
index sql-server execution-plan index-tuning query-performance
根据执行计划,你知道这个sql代码是否有效的衡量标准是什么?
根据执行计划如何知道这个sql代码是否高效?



SELECT [CustomerID], [CompanyName], [City], [Region]
FROM [Northwind].[dbo].[Customers]
WHERE [Country] = 'Germany'
ORDER BY [CompanyName]
SELECT [CustomerID], [CompanyName], [City], [Region]
FROM [Northwind].[dbo].[Customers]
WHERE [Country] = 'Germany'
Run Code Online (Sandbox Code Playgroud) performance index sql-server-2008 execution-plan query-performance
execution-plan ×10
sql-server ×7
index ×3
performance ×3
postgresql ×2
constraint ×1
errors ×1
hints ×1
index-tuning ×1
optimization ×1