Analyze the cause of inefficiency when the TOP statement is placed outside the table Value Function

Source: Internet
Author: User
Tags sql server query

SQLSERVER's table value function is a new feature of SQLSERVER 2005. It is easy to use, just like a separate table, and is widely used in our system. There is an SQL Server Table value function for obtaining customer data. If you log on as an administrator, this function will return rows of records, which takes about 30 seconds, however, if the TOP statement is placed outside the table value function, the efficiency is very low. It takes about 3 minutes:

 
 
  1. select top 20  * from GetFrame_CustomerSerch('admin','1') 

The following is the definition of the stored procedure:

 
 
  1. Alter function [dbo]. [GetFrame_CustomerSerch]
  2. (
  3. -- Add the parameters for the function here
  4. @ WorkNo varchar (38)
  5. , @ SerchChar varchar (500)
  6. )
  7. RETURNS TABLE
  8. AS
  9. RETURN
  10. (
  11. -- Add the SELECT statement with parameter references here
  12. Select a. GUID, a. CustomerName, a. CustomerIDcard, a. CustomerPhone, a. CustomerMobile from
  13. (
  14. -- The specific subquery is omitted
  15. )
  16. ) A union all
  17. Select B. GUID, B. CustomerName, B. CustomerIDcard, B. CustomerPhone, B. CustomerMobile from WFT_ManagerCollectUsers a left join WFT_Customer B on a. FundAccount = B. FundAccount
  18. -- Where a. WorkNo = @ WorkNo
  19. WHERE a. WorkNo IN
  20. (
  21. -- The specific subquery is omitted
  22. )
  23. )

This statement is placed in the SQL-MAP file of the PDF. NET data development framework, initially thought it was caused by the Framework, the statement is directly queried in the query analyzer, still very slow.

Extract the SQL statement in GetFrame_CustomerSerch and add the Top query directly. It takes only 6 seconds, N times faster:

 
 
  1. Declare @ WorkNo varchar (38)
  2. Declare @ SerchChar varchar (500)
  3. Set @ WorkNo = 'admin'
  4. Set @ SerchChar = '1'
  5. Select top 20 a. GUID, a. CustomerName, a. CustomerIDcard, a. CustomerPhone, a. CustomerMobile from
  6. (
  7. -- The specific subquery is omitted
  8. )
  9. ) A union all
  10. Select B. GUID, B. CustomerName, B. CustomerIDcard, B. CustomerPhone, B. CustomerMobile from WFT_ManagerCollectUsers a left join WFT_Customer B on a. FundAccount = B. FundAccount
  11. WHERE a. WorkNo IN
  12. (
  13. -- The specific subquery is omitted
  14. )

Why is there such a big difference?

My analysis may be due to the following reasons:

1. If Top or other conditions are used outside the table value function, the SQL Server Query Optimizer cannot optimize the query. For example, all records are returned first, then, select the first 20 records in the temporary table;

2. Although the table value function uses "Table variables", it is in memory, but if the "table" results are very large, it is very likely that the memory will not be retained, but the results will be stored in the physical memory, and the database will reserve some memory space for other queries), using virtual memory, the virtual memory is actually a disk page file. When there are too many records, frequent page exchanges will occur, resulting in a very low query efficiency.

It seems that the "Table value function" is not as good as the legend. I don't know what you think.

Recently, I also encountered a strange problem. There was a stored procedure that had always become extremely slow after 1-2 days of system operation, but it was very fast to modify it again, just adding a space and so on ), I don't know if you have met me. Why?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.