简体   繁体   English

如何避免在sql中进行全表扫描

[英]How to avoid full table scan in sql

    SELECT 
      DISTINCT P.IDENTIFICATIONNUM IDNUMBER, 
      P.NAME NAME, 
      P.NATIONALITY NATIONALITY, 
      O.NAME COMPANY
    FROM APPLICANT_TB P
    LEFT JOIN APP_TB A ON A.APPLICANTID=P.APPLICANTID
    LEFT JOIN ORGANISATION_TB O ON O.ORGID = A.ORGID

as the sql code showing, i am using IBM DB2 , and according to explain plan , all the 3 tables are full table scan .can someone tell me how to avoid this ? 正如sql代码所示,我正在使用IBM DB2,并根据解释计划,所有3个表都是全表扫描。有人告诉我如何避免这种情况? (all the PK using are indexed) (所有PK使用都被编入索引)

Be more selective with the records you want. 对所需记录更具选择性。 Include a WHERE clause. 包含WHERE子句。

Since you are selecting all the rows the most efficient way to return those to you is to do table scans. 由于您选择了所有行,因此最有效的方法是将这些行返回给您进行表扫描。

Even if you add filters you still may do table scans, that will depend on how well your indexes match to the columns you are filtering on, and how up-to-date the database statistics are. 即使您添加过滤器,您仍然可以进行表扫描,这取决于索引与要过滤的列的匹配程度,以及数据库统计信息的最新状态。

In general the query optimizer will guess the percentage of the table it needs based on you filter. 通常,查询优化器会根据您的过滤器猜测所需表的百分比。 Once that percentage goes over a certain (surprisingly small like 20%) portion of the table, it will choose a table scan as the "best" way to get the data you are asking for. 一旦该百分比超过表格的某个(令人惊讶的小,如20%)部分,它将选择表格扫描作为获取您要求的数据的“最佳”方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM