我的查询是他们使用100%CPU。我试图解决它们,但由于我不是数据库专家,它真的变得更糟。
首先我的脚本执行此查询。
SELECT *
FROM
(SELECT
[e].[event_id],
[e].[event_name],
[e].[event_datetime],
[v].[name] [venue_name],
[v].[city],
[s].[state_name],
[v].[venue_id],
ROW_NUMBER() OVER (ORDER BY [event_datetime]) AS RowNum
FROM
[indux].[dbo].[event] [e]
JOIN
[indux].[dbo].[venue] [v] ON [e].[venue_id] = [v].[venue_id]
JOIN
[indux].[dbo].[system_state] [s] ON [v].[state_id] = [s].[state_id]
WHERE
[e].[event_id] IN (SELECT DISTINCT event_id
FROM [indux].[dbo].ticket_group
WHERE ticket_group_id IN (SELECT DISTINCT ticket_group_id
FROM [indux].[dbo].[ticket] [t]
WHERE [t].[actual_sold_price] = -1)
)
AND [e].[event_datetime] >= '2014/08/09'
AND [e].[event_datetime] <= '2014/12/09'
) AS MyDerivedTable
WHERE
MyDerivedTable.RowNum BETWEEN 0 AND 250
并且在同一时间我执行以下查询,而不是将两者的结果结合起来使用PHP在我的页面中显示
SELECT
COUNT([e].[event_id])
FROM
[indux].[dbo].[event] [e]
WHERE
[e].[event_id] IN (SELECT DISTINCT event_id
FROM [indux].[dbo].ticket_group
WHERE ticket_group_id IN
(SELECT DISTINCT ticket_group_id
FROM [indux].[dbo].[ticket] [t]
WHERE [t].[actual_sold_price] = -1)
)
AND [e].[event_datetime] >= '$today'
AND [e].[event_datetime] <= '$to'
任何人都知道我是否可以修复这些查询?我认为第二个查询中的计数是个问题。
答案 0 :(得分:0)
您需要在故障单组ID上加入故障单组和故障单,并将其加入到事件ID的主查询中。
嵌套查询对于嵌套了where子句的查询的每一行都运行一次。
嵌套两次就像在循环中运行循环一样,整个事情必须运行一次,并且每次查询返回一行时都要将值与值进行比较。从您的选择中可以明显看出,您可以加入这些价值观。
SELECT *Whatever*
FROM [indux].[dbo].[event] [e]
JOIN [indux].[dbo].[venue] [v]
ON [e].[venue_id] = [v].[venue_id]
JOIN [indux].[dbo].[system_state] [s]
ON [v].[state_id] = [s].[state_id]
JOIN indux.dbo.ticket_group tg on tg.event_id=r.event_id
JOIN indux.dbo.ticket t on t.ticket_group_id=tg.ticket_group_id
WHERE t.actual_sold_price=-1
SELECT COUNT(e.event_id)
FROM dbo.event e
JOIN indux.dbo.ticket_group tg on tg.event_id=e.event_id
JOIN indux.dbo.ticket t on t.ticket_group_id=tg.ticket_group_id
WHERE e.event_datetime >= '$today'
AND e.event_datetime <= '$to'
编辑清晰度
SELECT *Whatever*
FROM [indux].[dbo].[event] [e]
JOIN [indux].[dbo].[venue] [v]
ON [e].[venue_id] = [v].[venue_id]
JOIN [indux].[dbo].[system_state] [s]
ON [v].[state_id] = [s].[state_id]
JOIN (
SELECT DISTINCT tg.event_id
FROM indux.dbo.ticket_group tg
JOIN indux.dbo.ticket t on t.ticket_group_id=tg.ticket_group_id
WHERE actual_sold_price = -1) de
on de.Event_id=e.event_id
使用临时表即
CREATE TABLE #EventID (id int)
INSERT INTO #EventID SELECT tg.event_id etc...
将允许您重用表而不是重新查询count语句。