提高从 View 获取数据的查询性能
Posted
技术标签:
【中文标题】提高从 View 获取数据的查询性能【英文标题】:Improve performance of query fetching data from View 【发布时间】:2012-12-24 09:40:16 【问题描述】:我有一个 sql 查询,它只从 View 中获取一条记录。我的观点是由很多功能组成的。它返回大约 60,000 条记录。 我想从这个视图中获取的最高记录需要 8-9 秒。 我如何优化我的视图,使其最多需要 1-2 秒。 这是我的视图和sql查询。请帮忙!任何建议都将是可观的。提前致谢。
CREATE View dbo.OMOrderPrePaymentINT
As
select
dbo.int_payment_customer_number_out('','OMOrderPaymentMasterINT',payment_id)
as customer_number,
dbo.int_customer_name_out('','OMOrderPaymentMasterINT',payment_id)
as customer_name,
dbo.int_FormatDate('','OMOrderPaymentMasterINT',document_date)
as payment_date,
dbo.int_payment_amount_out('','OMOrderPaymentMasterINT',document_amount)
as payment_amount,
dbo.int_checkbook_id_out('','OMOrderPaymentMasterINT',null)
as checkbook_id,
dbo.int_cheque_number_out('','OMOrderPaymentMasterINT',payment_id)
as cheque_number,
dbo.int_cc_type_out ('','OMOrderPaymentMasterINT',payment_id)
as cc_type,
dbo.int_cc_number_out('','OMOrderPaymentMasterINT',payment_id)
as cc_number,
dbo.int_cc_authcode_out('','OMOrderPaymentMasterINT',payment_id)
as cc_authcode,
dbo.int_ach_account_type_out('','OMOrderPaymentMasterINT',payment_id)
as ach_account_type,
dbo.int_ach_account_number_out('','OMOrderPaymentMasterINT',payment_id)
as ach_account_number,
dbo.int_ach_authcode_out('','OMOrderPaymentMasterINT',payment_id)
as ach_authcode,
dbo.int_expiration_date_out('','OMOrderPaymentMasterINT',payment_id)
as creditcard_expiration_date,
dbo.int_order_payment_type_out('','OMOrderPaymentMasterINT',payment_id)
as payment_type,
dbo.int_payment_method_out('','OMOrderPaymentMasterINT',payment_id)
as payment_method,
dbo.int_erp_payment_action_out('','OMOrderPaymentMasterINT',null)
as [action],
dbo.int_modified_user_id_out('','',null)
as modified_user_id,
'NOT MAPPED'
as void_date,
export_completed
as exportcompleted,
dbo.int_ordergroup_id_out('','OMOrderPaymentMasterINT',payment_id)
as ordergroup_id,
'ECOM'
as USRDEFND1,
dbo.int_ResponseToken_out('','OMOrderPaymentMasterINT',payment_id)
as USRDEFND4,
dbo.int_transaction_id_out('','OMOrderPaymentMasterINT',payment_id)
as USRDEFND5
from PaymentLine
where dbo.int_exportPayment(payment_id) = 1
SQL 查询
select top 1 *
from OMOrderPrePaymentINT
where ordergroup_id = '943177C1-50B6-4E7C-A442-BA90CF2A03F6'
order by payment_date desc
【问题讨论】:
【参考方案1】:哎哟。
用户定义的函数在 SQL server 中被称为the worst performance offenders,您的视图使用了很多。 UDF在row-by-row模式下工作,所以需要一段时间。
尽量用内联 SQL 替换它们。
【讨论】:
【参考方案2】:内联 sql 不能对函数做什么?每个函数调用都将使用该给定函数的查询执行计划。如果您使用内联 SQL 代替函数调用,则将使用一个整体(预计最有效)查询执行计划。
【讨论】:
以上是关于提高从 View 获取数据的查询性能的主要内容,如果未能解决你的问题,请参考以下文章
从 Android ContactsContract.Contacts 查询所有联系人的许多数据时如何提高性能?
lucene3.0 通过IndexSearcher.doc(i)获取Document 非常慢,如何提高性能?