在特定情况下创建索引的最有效方法是什么

时间:2017-07-02 15:11:54

标签: sql database oracle indexing sql-tuning

好的,这是我的条件和问题。假设这是我们用于表/索引的唯一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
  1. 为每个表创建索引最有效的是什么?

    -CUST:COUNTRY_CD + CUST_ID

    -DIST:CUST_CD + DIST_NM

    -ORDS:CUST_ID + ORDER_CD

    -CUST_INFO:INFO + CUST_ID

  2. --->我认为上述指数是最有效的指标。但是还有什么我应该考虑的吗?

    1. 根据上面的索引编制执行计划会是什么样子? (加入命令?方法?子查询角色?,如果结果相同,可以更改sql)
    2. 请你解释一下这个... 我在想 1.从CUST_INFO中选择CUST_ID WHERE INFO第一次执行的行数最少, 2.然后加入CUST表, 3.然后使用SELECT CUST_ID FROM ORDS WHERE ORDER_CD =' 20'子查询输出, 4.然后加入DIST表,然后执行分组...

      我是否在正确的轨道上?谁能给我一个建议?

2 个答案:

答案 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如何处理这项任务:

  1. 阅读ORDSindex idx1 on ords (order_cd, cust_id)
  2. 阅读CUST_INFOindex idx2 on cust_info (info, cust_id)
  3. 阅读CUSTindex idx3 on cust (country_cd, cust_id, cust_cd)index idx4 on cust (cust_id, country_cd, cust_cd)
  4. 加入DISTindex idx5 on dist (cust_cd, dist_nm)
  5. 或者也许如此:

    1. 阅读CUSTindex idx3 on cust (country_cd, cust_id, cust_cd)index idx6 on cust (country_cd, cust_cd, cust_id)
    2. 查找ORDSindex idx7 on cust (cust_id, order_cd)
    3. 查找CUST_INFOindex idx8 on cust_info (cust_id, info)
    4. 加入DISTindex idx5 on dist (cust_cd, dist_nm)
    5. 这使得可能使用或不使用8个索引。创建它们,使用EXPLAIN PLAN查看使用的内容,然后删除其他内容: - )