gpt4 book ai didi

mysql - 非常大的 SQL 查询的问题

转载 作者:可可西里 更新时间:2023-11-01 07:59:32 26 4
gpt4 key购买 nike

问题:

我正在创建一个 Trac 报告,显示在我们图书馆的每个章节的开发周期的每个阶段有多少票。 一张工单代表一项工作,通常是一个单独的例程。

例如,在第 X 章的同行评审阶段,即将发布(里程碑)的门票有多少。

共有 10 个发展阶段和 47 个章节。

给定的 MySQL 查询适用于所有 10 个开发阶段,但仅适用于一章且长 25 行,因此所有章节的整个查询超过 1200 行。

Trac 给出的错误是 KeyError: 'numrows' 查询变得很大。

当直接在 MySQL 中输入查询时,给出的错误是 Out of resources when opening file (Errcode: 24) (23)

问题:

  • 重构 - 这可以做得“更好”吗?sql 大师们,有一些聪明的技巧/高级技术吗?

  • 方法 - 我是否需要完全不同的方法?

  • 配置 - MySQL 和/或 Trac 是否可以配置为接受非常大的查询

注释:

表中的数据很小,在明显的大小限制下执行查询不会花费很长时间。

查询从 Trac 系统传递到 MySQL,这对可以执行的操作施加了一些限制,例如,只能从 trac 发送单个查询以生成报告。

可以查看 Trac 报告的示例 here.

查询中的 %c%* 只是我在通过脚本生成查询时用于替换实际章节的唯一字符串。

SELECT '%c%' as Chapter,
(SELECT count(ticket.id) AS Matches FROM engine.ticket INNER JOIN engine.ticket_custom ON ticket.id = ticket_custom.ticket
WHERE ticket_custom.name='chapter' AND ticket_custom.value LIKE '%c%' AND type='New material' AND milestone='1.1.12' AND component NOT LIKE 'internal_engine' AND ticket.status IN ('new','assigned') ) AS 'New',
(SELECT count(ticket.id) AS Matches FROM engine.ticket INNER JOIN engine.ticket_custom ON ticket.id = ticket_custom.ticket
WHERE ticket_custom.name='chapter' AND ticket_custom.value LIKE '%c%' AND type='New material' AND milestone='1.1.12' AND component NOT LIKE 'internal_engine' AND ticket.status='document_interface' ) AS 'Document\
Interface',
(SELECT count(ticket.id) AS Matches FROM engine.ticket INNER JOIN engine.ticket_custom ON ticket.id = ticket_custom.ticket
WHERE ticket_custom.name='chapter' AND ticket_custom.value LIKE '%c%' AND type='New material' AND milestone='1.1.12' AND component NOT LIKE 'internal_engine' AND ticket.status='interface_development' ) AS 'Inter\
face Development',
(SELECT count(ticket.id) AS Matches FROM engine.ticket INNER JOIN engine.ticket_custom ON ticket.id = ticket_custom.ticket
WHERE ticket_custom.name='chapter' AND ticket_custom.value LIKE '%c%' AND type='New material' AND milestone='1.1.12' AND component NOT LIKE 'internal_engine' AND ticket.status='interface_check' ) AS 'Interface C\
heck',
(SELECT count(ticket.id) AS Matches FROM engine.ticket INNER JOIN engine.ticket_custom ON ticket.id = ticket_custom.ticket
WHERE ticket_custom.name='chapter' AND ticket_custom.value LIKE '%c%' AND type='New material' AND milestone='1.1.12' AND component NOT LIKE 'internal_engine' AND ticket.status='document_routine' ) AS 'Document R\
outine',
(SELECT count(ticket.id) AS Matches FROM engine.ticket INNER JOIN engine.ticket_custom ON ticket.id = ticket_custom.ticket
WHERE ticket_custom.name='chapter' AND ticket_custom.value LIKE '%c%' AND type='New material' AND milestone='1.1.12' AND component NOT LIKE 'internal_engine' AND ticket.status='full_development' ) AS 'Full Devel\
opment',
(SELECT count(ticket.id) AS Matches FROM engine.ticket INNER JOIN engine.ticket_custom ON ticket.id = ticket_custom.ticket
WHERE ticket_custom.name='chapter' AND ticket_custom.value LIKE '%c%' AND type='New material' AND milestone='1.1.12' AND component NOT LIKE 'internal_engine' AND ticket.status='peer_review_1' ) AS 'Peer Review O\
ne',
(SELECT count(ticket.id) AS Matches FROM engine.ticket INNER JOIN engine.ticket_custom ON ticket.id = ticket_custom.ticket
WHERE ticket_custom.name='chapter' AND ticket_custom.value LIKE '%c%'AND type='New material' AND milestone='1.1.12' AND component NOT LIKE 'internal_engine' AND ticket.status='peer_review_2' ) AS 'Peer Review Tw\
o',
(SELECT count(ticket.id) AS Matches FROM engine.ticket INNER JOIN engine.ticket_custom ON ticket.id = ticket_custom.ticket
WHERE ticket_custom.name='chapter' AND ticket_custom.value LIKE '%c%' AND type='New material' AND milestone='1.1.12' AND component NOT LIKE 'internal_engine' AND ticket.status='qa' ) AS 'QA',
(SELECT count(ticket.id) AS Matches FROM engine.ticket INNER JOIN engine.ticket_custom ON ticket.id = ticket_custom.ticket
WHERE ticket_custom.name='chapter' AND ticket_custom.value LIKE '%c%'AND type='New material' AND milestone='1.1.12' AND component NOT LIKE 'internal_engine' AND ticket.status='closed' ) AS 'Closed',
count(id) AS Total,
ticket.id AS _id
FROM engine.ticket
INNER JOIN engine.ticket_custom ON ticket.id = ticket_custom.ticket
WHERE ticket_custom.name='chapter' AND ticket_custom.value LIKE '%c%' AND type='New material' AND milestone='1.1.12' AND component NOT LIKE 'internal_engine'

最佳答案

不是对每个计数都进行子查询,而是使用 case 从已为查询获取的数据中计数:

select '%c%' as Chapter,
sum(case when ticket.status IN ('new','assigned') then 1 else 0 end) as 'New',
sum(case when ticket.status='document_interface' then 1 else 0 end) as 'DocumentInterface',
sum(case when ticket.status='interface_development' then 1 else 0 end) as 'Interface Development',
sum(case when ticket.status='interface_check' then 1 else 0 end) as 'Interface Check',
sum(case when ticket.status='document_routine' then 1 else 0 end) as 'Document Routine',
sum(case when ticket.status='full_development' then 1 else 0 end) as 'Full Development',
sum(case when ticket.status='peer_review_1' then 1 else 0 end) as 'Peer Review One',
sum(case when ticket.status='peer_review_2' then 1 else 0 end) as 'Peer Review Two',
sum(case when ticket.status='qa' then 1 else 0 end) as 'QA',
sum(case when ticket.status='closed' then 1 else 0 end) as 'Closed',
count(id) as Total,
ticket.id as _id
from
engine.ticket
inner join engine.ticket_custom on ticket.id = ticket_custom.ticket
where
ticket_custom.name='chapter' and
ticket_custom.value LIKE '%c%' and
type='New material' and
milestone='1.1.12' and
component NOT LIKE 'internal_engine'

关于mysql - 非常大的 SQL 查询的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12607667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com