好的,这是我的条件和问题。假设这是我们用于表/索引的唯一SQL
SELECT B.DIST_NM
, COUNT(+)
FROM CUST A, DIST B
WHERE A.COUNTRY_CD = 'USA'
AND A.CUST_CD = B.CUST_CD
AND A.CUST_ID IN (SELECT CUST_ID FROM ORDS WHERE ORDER_CD = '20')
AND A.CUST_ID IN (SELECT CUST_ID FROM CUST_INFO WHERE INFO = 'N')
GROUP BY B.DIST_NM
........................................
SELECT COUNT(*) FROM CUST WHERE A.COUNTRY_CD = 'USA' -> about half million rows
SELECT CUST_ID FROM ORDS WHERE ORDER_CD = '20' -> about 1.5 million rows
SELECT CUST_ID FROM CUST_INFO WHERE INFO = 'N' -> about 50 rows
为每个表创建索引最有效的是什么?
-CUST:COUNTRY_CD + CUST_ID
-DIST:CUST_CD + DIST_NM
-ORDS:CUST_ID + ORDER_CD
-CUST_INFO:INFO + CUST_ID
--->我认为上述指数是最有效的指标。但是还有什么我应该考虑的吗?
请你解释一下这个... 我在想 1.从CUST_INFO中选择CUST_ID WHERE INFO第一次执行的行数最少, 2.然后加入CUST表, 3.然后使用SELECT CUST_ID FROM ORDS WHERE ORDER_CD =' 20'子查询输出, 4.然后加入DIST表,然后执行分组...
我是否在正确的轨道上?谁能给我一个建议?
答案 0 :(得分:1)
我会将查询写成:
SELECT d.DIST_NM, COUNT(*)
FROM CUST c JOIN
DIST d
ON c.CUST_CD = d.CUST_CD
WHERE c.COUNTRY_CD = 'USA' AND
c.CUST_ID IN (SELECT o.CUST_ID FROM ORDS o WHERE o.ORDER_CD = '20')
c.CUST_ID IN (SELECT ci.CUST_ID FROM CUST_INFO ci WHERE ci.INFO = 'N')
GROUP BY d.DIST_NM;
我建议使用索引:CUST(COUNTRY_CD, CUST_ID, CUST_CD)
,DIST(CUST_CD, DIST_NM)
,ORDS(ORDER_CD, CUST_ID)
和CUST_INFO(INFO, CUST_ID)
。
请注意索引中键的顺序。
答案 1 :(得分:0)
让我们考虑一下DBMS如何处理这项任务:
ORDS
:index idx1 on ords (order_cd, cust_id)
CUST_INFO
:index idx2 on cust_info (info, cust_id)
CUST
:index idx3 on cust (country_cd, cust_id, cust_cd)
或index idx4 on cust (cust_id, country_cd, cust_cd)
DIST
:index idx5 on dist (cust_cd, dist_nm)
或者也许如此:
CUST
:index idx3 on cust (country_cd, cust_id, cust_cd)
或index idx6 on cust (country_cd, cust_cd, cust_id)
ORDS
:index idx7 on cust (cust_id, order_cd)
CUST_INFO
:index idx8 on cust_info (cust_id, info)
DIST
:index idx5 on dist (cust_cd, dist_nm)
这使得可能使用或不使用8个索引。创建它们,使用EXPLAIN PLAN
查看使用的内容,然后删除其他内容: - )