曾几何时,有一个包含许多联接的视图:
CREATE VIEW [dbo].[V_BIGGEST_VIEW_EVER]
AS
SELECT {many many columns}
FROM (SELECT * FROM dbo.T_CUS_TSK_TASK WHERE is_deleted=0) T
INNER JOIN dbo.V_CUS_GRP_GROUP G ON (T.group_id = G.group_id)
INNER JOIN dbo.T_BKK_DISCOUNT_TYPE DT ON (DT.discount_type_id=T.discount_type_id)
INNER JOIN dbo.T_BKK_CURRENCY DC ON (T.debit_currency_id=DC.currency_id)
INNER JOIN dbo.T_BKK_CURRENCY PC ON (T.payback_currency_id=PC.currency_id)
INNER JOIN dbo.T_BKK_CURRENCY FC ON (T.final_debit_currency_id=FC.currency_id)
INNER JOIN dbo.T_GLOBAL_COUNTER D1C ON (D1C.company_id=T.company_id AND
D1C.counter_name='PROFORMA_INVOICE_COUNTER')
INNER JOIN dbo.T_GLOBAL_COUNTER D2C ON (D2C.company_id=T.company_id AND
D2C.counter_name='TAX_INVOICE_COUNTER')
INNER JOIN dbo.T_GLOBAL_COUNTER D3C ON (D3C.company_id=T.company_id AND
D3C.counter_name='INVOICE_RECEIPT_COUNTER')
INNER JOIN dbo.T_GLOBAL_COUNTER D4C ON (D4C.company_id=T.company_id AND
D4C.counter_name='DELIVERY_NOTE_COUNTER')
INNER JOIN dbo.T_GLOBAL_COUNTER D5C ON (D5C.company_id=T.company_id AND
D5C.counter_name='BILL_OF_LADING_COUNTER')
INNER JOIN dbo.T_GLOBAL_COUNTER D6C ON (D6C.company_id=T.company_id AND
D6C.counter_name='CREDIT_INVOICE_COUNTER')
LEFT JOIN dbo.V_SYS_BRANCH BR ON (T.branch_id = BR.branch_id)
LEFT JOIN dbo.T_CUS_TSK_TASKS_ARRAY AR ON (T.array_id = AR.array_id)
LEFT JOIN dbo.T_DRIVER D ON (T.driver_id = D.driver_id)
LEFT JOIN dbo.T_VEHICLE V ON (T.vehicle_id = V.vehicle_id)
LEFT JOIN dbo.T_STF_INVITER I ON (T.inviter_id = I.inviter_id)
LEFT JOIN dbo.T_STF_SUBCONTRACTOR SC1 ON (SC1.subcontractor_id = D.subcontractor_id)
LEFT JOIN dbo.T_STF_SUBCONTRACTOR SC2 ON (SC2.subcontractor_id = T.subcontractor_id)
LEFT JOIN dbo.T_CUS_TSK_TASK_STATUS S ON (S.task_status_id=T.task_status_id)
LEFT JOIN dbo.V_STF_SUB_LOCATION SL1 ON (SL1.sub_location_id=T.start_sub_location_id)
LEFT JOIN dbo.V_STF_SUB_LOCATION SL2 ON (SL2.sub_location_id=T.end_sub_location_id)
LEFT JOIN dbo.T_STF_CUSTOMER CU ON (CU.customer_id=T.customer_id)
LEFT JOIN dbo.T_STF_CUSTOMER_SPLITTING_CODE SP ON (SP.splitting_id=T.splitting_id)
LEFT JOIN dbo.V_CUS_TSK_CREDIT_FOR_TASK CR ON CR.task_id=T.task_id
LEFT JOIN dbo.T_BKK_PROFORMA_INVOICE D1 ON (T.proforma_invoice_id=D1.proforma_invoice_id)
LEFT JOIN dbo.T_BKK_TAX_INVOICE D2 ON (T.tax_invoice_id=D2.tax_invoice_id)
LEFT JOIN dbo.T_BKK_INVOICE_RECEIPT D3 ON (T.invoice_receipt_id=D3.invoice_receipt_id)
LEFT JOIN dbo.T_BKK_DELIVERY_NOTE D4 ON (T.delivery_note_id=D4.delivery_note_id)
LEFT JOIN dbo.T_BKK_BILL_OF_LADING D5 ON (T.bill_of_lading_id=D5.bill_of_lading_id)
LEFT JOIN dbo.V_CUS_TSK_CONTAINER CONTAINER1 ON (CONTAINER1.container_id=T.container1_id)
LEFT JOIN dbo.V_CUS_TSK_CONTAINER CONTAINER2 ON (CONTAINER2.container_id=T.container2_id)
LEFT JOIN dbo.V_STF_TRAILER TRAILER1 ON (TRAILER1.trailer_id=T.trailer1_id)
LEFT JOIN dbo.V_STF_TRAILER TRAILER2 ON (TRAILER2.trailer_id=T.trailer2_id)
LEFT JOIN dbo.T_STF_LUGGAGE_TYPE LUGGAGE_TYPE ON (LUGGAGE_TYPE.luggage_type_id=T.luggage_type_id)
有一天,用户向视图询问了查询:
SELECT {many many columns}
FROM V_BIGGEST_VIEW_EVER
WHERE {column1}=1 AND
{column2}=2 AND
.......
{and so and so}
.......
{columnN}=N
懒惰的最大观点 - 曾经工作和工作过 5分钟(!!)并且不会少于它返回结果。
这些表有主键和外键。
如何减少查询执行的时间? 我该如何减少这种观点?
我在Google上搜索过,但找不到任何帮助。
答案 0 :(得分:3)
让我们考虑一下,查询代表了有效的业务需求。
仅仅因为视图很大并不意味着它应该表现不佳。从该视图中选择的性能主要由基础表的布局驱动。即使有一个视图左连接超过20个查找表,SQL Server应该以毫秒为单位返回结果,前提是表T_CUS_TSK_TASK
已正确索引以执行查询。
您必须像任何其他查询优化一样处理此问题。调查主要IO因素(SET STATISTICS IO ON
)是什么,调查查询计划,查看基数估计,考虑统计数据是否正确,查看缺少索引提示的查询并考虑如何更改表格架构相应。你的起点应该是:Designing Indexes。即使粗略地看一下你的(在post中没有提供)表模式,也应该揭示,如果deleted
不是最左边的聚簇索引键那么你肯定有问题。
您目前根据其文字盲目攻击查询的做法完全不专业。
现在当然 hard 认为此查询代表了有效的业务需求。但是,您对查询优化和数据模型设计('那些表有主键和外键')的看法是原始的,使用温和的术语。阅读有关索引设计,阅读有关索引,购买书籍(如:Inside Microsoft SQL Server 2005: Query Tuning and Optimization)。