SELECT dbo.order_detail.doc_no, dbo.Product_Master.Model, dbo.Product_Master.Color, 
      dbo.order_detail.qty * dbo.order_detail.unit_price AS total_price, 
      dbo.Product_Master.description, dbo.Product_Master.stop_date, 
      dbo.workflow_detail.Result, dbo.workflow_detail.wf_no, dbo.So_Master.so_no, 
      dbo.So_Master.customer_code, dbo.So_Master.customer_name, 
      dbo.So_Master.so_date, dbo.So_Master.sales, dbo.order_detail.unit_price, 
      dbo.So_Master.trans_fee, dbo.So_Master.ignore, dbo.So_Master.financial, 
      dbo.So_Master.payment1, dbo.So_Master.cartage, dbo.So_Master.curr, 
      dbo.So_Master.tax, dbo.So_Master.TOTALAMOUNT
FROM dbo.Customer_Master INNER JOIN
      dbo.So_Master ON 
      dbo.Customer_Master.customer_code = dbo.So_Master.customer_code INNER JOIN
      dbo.workflow_master INNER JOIN
      dbo.workflow_detail ON 
      dbo.workflow_master.wf_no = dbo.workflow_detail.wf_no INNER JOIN
      dbo.Product_Master INNER JOIN
      dbo.order_detail ON dbo.Product_Master.Part_no = dbo.order_detail.part_no ON 
      dbo.workflow_master.doc_no = dbo.order_detail.doc_no ON 
      dbo.So_Master.so_no = dbo.order_detail.doc_no
这是我做的最基本的查询,求高手指点,提高其查询性能

解决方案 »

  1.   

    你不用where限定任何条件?那这样的话,这么多表笛卡尔乘积,不慢才怪。只用到需要的数据来join,可以大大提高效率。
      

  2.   

    if keyword <> "" then
    SQLComm=SQLComm & " and (Scustomer_name like N'%" & keyword & "%' or so_no like '%" & keyword & "%')"
    end if
    if account <> "t" then
    SQLComm = SQLComm & "and sales = '"& account &"' "
    end if
    if stype <> "t" then
        SQLComm = SQLComm & "and Result='" & stype & "'"
        end if
    if Ignore_type = "N" then
    SQLComm = SQLComm & " And ignore is null "
    end if
    if Financial_type = "N" then
    SQLComm = SQLComm & " And financial is null "
    end if
    SQLComm = SQLComm & " Order By so_date Desc, so_no Desc"
    if Request.Form <> "" then 
    set rsCate = DBConn.Execute(SQLComm)
    else
    set rsCate = DBConn.Execute("select so_no from [so_master] where 1 <> 1 ")
    end if
    我后面是有条件的。但还是很慢,大家帮忙把上边的sql优化一下
      

  3.   

    你可以在sqlserver里面查看下执行计划,根据执行计划来优化
      

  4.   

    还没用过索引,常用的多表联合查询的sql语句:
    select table1.ID from table1 inner join table2 on table1.ID=table2.ID
    where table2.col2='xxx'改进后:
    select a.ID from table1 a,
    (select col1,ID from table2 where col2='xxx') b
    where a.ID=b.ID
    看看这个怎么按这个优化