Access 2007: Subqueries causing massive performance loss

Deadly 提交于 2020-01-16 14:46:10

问题


I am building a query to search for records matching any number of fields in a table with 15+ columns. (All fields for which an input is given must match.)

However, the original table was badly designed, in some cases having upwards of ten fields for the same thing. (With names such as Street_11.) I have separated the data into multiple tables such that Street_2 through Street_12 are now all labeled under Street_2 in a separate table, containing only that column and fileID, which is taken from the main table's primary key. (There is no formal relation between the two, though fileID in the secondary table serves as a foreign key.)

Prior to the normalization efforts, the search form was using this query. While not optimal, it was at least functional. I have since removed the segments searching the Street_X and Block_X fields, replacing them with subqueries:

WHERE 
(
    [Map index].fileID IN 
    (
    SELECT fileID FROM [fileID-Street] 
    WHERE [fileID-Street].Street_2 LIKE "*" & [Forms]![DW_Form]![Street] & "*"
    ) 
  OR 
    ([Forms]![DW_Form]![Street] Is Null)
)
AND 
(
    [Map index].fileID IN 
    (
    SELECT fileID FROM [fileID-blockLot] 
    WHERE [fileID-blockLot].Block LIKE "*" & [Forms]![DW_Form]![Street] & "*"
    ) 
OR 
    ([Forms]![DW_Form]![Block] Is Null)
)

The above queries search the smaller tables. However, when including these in the main query (between the FROM and current WHERE clauses, with the current WHERE replaced with AND)I have encountered a truly staggering loss of performance; including just the Street clause (with relevant section of the original query removed) raises processing time from ~5s to ~45s. Adding both subqueries slows the search time to well over three minutes.

At this point it's clear that I'm likely going about this the wrong way; what information I've been able to find (such as here) seems to indicate that the subqueries might be running on the entire subtable for every record. What should I be doing in this situation?


回答1:


Try using a join instead of a subquery, it generally performs better. Example:

SELECT ...
FROM [Map index] LEFT OUTER JOIN [fileID-Street] 
ON [Map index].fileID=[fileID-Street].fileID
WHERE ... AND [fileID-Street].Street_2 LIKE '*" & [Forms]![DW_Form]![Street] & "*'


来源:https://stackoverflow.com/questions/31816055/access-2007-subqueries-causing-massive-performance-loss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!