1

是否可以优化此查询?

SELECT 
    tr.transferfrom, tr.transferto, br.id as 'BR_ID',tr.refno, 
    br.name as 'BR_NAME', br.code as 'BRANCH_CODE', tr.docno, tr.transdate,
    stk.stockno,stk.salescat,tr.qty, tr.sprice,tr.qty*tr.sprice as 'GROSS_SALES' 
FROM 
    TRANSFER tr 
JOIN 
    branch br on tr.branchid = br.id 
JOIN 
    stocks stk on tr.stockid=stk.id 
WHERE 
    RIGHT(tr.refno,7) = 'ST00576' AND LEFT(tr.refno,2) = 89 
    AND stk.stockno = 'NS4370 CR - L'; 
4

1 回答 1

1
  1. 创建索引tr.refno并替换LEFT(tr.refno,2) = 89tr.refno LIKE '89%'
  2. 创建复合索引stk.id + stk.stockno
  3. 确保br.id被索引覆盖
  4. 如果确实有很多记录(数亿)并且如果经常执行此查询 - 可能将最后 7 个tr.refno字符存储在单独的字段中以避免RIGHT(tr.refno,7) = 'ST00576'(不确定 sql server 是否支持反向索引)
于 2012-07-05T05:02:12.917 回答