SELECT
sum(CheckFinal.SUM) AS SUME,
strftime('%Y - %m', CheckDate) AS CheckDate
FROM
CheckFinal
WHERE CheckFinal.NUMER IN (
SELECT
CheckDetail.NUMER
FROM
CheckDetail
WHERE
CheckDetail.NUMER IN (
SELECT
PriceList.UniqID AS PriceListUniqID,
PriceList.Name AS PriceListName,
Category.UniqID
FROM
PriceList Join Category on PriceList.CATEGORY = Category.UniqID
WHERE (Category.UniqID = 2)
)
)
GROUP BY strftime('%Y %m', CheckDate);
我有这样的查询来组合4个表中的数据: - 类别(100条记录) - PriceList(20' 000记录) - CheckDetail(10' 000' 000记录) - CheckFinal(2' 000' 000记录)
简单来说,我正在寻找被列为Category.UniqID#2的子项的PriceList项目,然后我想收集所有CheckDetail.NUMER插件以定义具有此类PriceList项目的所有销售价值。此外,我正在寻找可以收集所有CheckFinal.NUMER的可行性。
我遇到的问题是:
WHERE (Category.UniqID = 2)
在我的情况下,我应该通过5&000; 000&000; 000条记录查找3到3 000次,但我有10套,查询将花费大约10个小时来编译。
JOIN会优化查询时间吗?如何制作这样的JOIN QUERY?
是否有任何GUI工具可以使用构造函数或类似的东西进行查询?
UPD :
http://sqlfiddle.com/#!5/50a93/2(使用SQL.js插入多行数据)
答案 0 :(得分:0)
WITH JOIN
,您的查询看起来像
SELECT
sum(CF.SUM) AS SUME,
strftime('%Y - %m', CF.CheckDate) AS CheckDate
FROM
PriceList
Join Category
on PriceList.CATEGORY = Category.UniqID
AND Category.UniqID = 2
JOIN CheckDetail CD
ON CD.NUMBER = PriceList.UniqID
JOIN CheckFinal CF
ON CF.NUMBER = CD.NUMBER
GROUP BY strftime('%Y - %m', CF.CheckDate);