Results 1 to 5 of 5
  1. #1
    TerraEarth is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Posts
    86

    Tips on increasing query speed (SQL) ?

    I'm having an issue where my queries are horrendously slow (1 minute + compilation time) when my database only has ~ 10k records with 15 or so columns.



    My style of usage is that I take an atomic approach: I take a complicated query that I need, find all the component, atomic parts and great them up into their own mini queries in Access. I then run the complicated query using the established mini queries.

    I've noticed that NOT IN especially is slow. I am frequently using key words such as GROUP BY, HAVING, MIN(), SUM(), NOT IN, IN, WHERE, etc. for more complex queries.

    Usually I wouldn't care if a query took 2-3 minutes to compile but these queries will be used for important features in the database so I feel that the run-time needs to be worked on. Any tips?

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    First step would be to show us :
    -plain English description of what your query is expected to do,
    -the SQL of the query involved, and
    -the design of tables involved showing keys/indexes.

    Good luck with your project.

  3. #3
    TerraEarth is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Posts
    86
    The query is built using a table documenting financial records.
    There's an entry in the table for each year that the record is open, with amounts (amt1, amt2, amt3, ..., amt6) for each relevant financial field
    the type column indicates what category the record falls under


    So a sample might look like the following:


    doc_num | year | amt1 | amt2 | ... | amt6 | type
    doc1 | 2016 | $1 | $1 | ... | $2 | category_x
    doc1 | 2017 | $1 | $1 | ... | $1 | category_x
    doc2 | 2017 | $2 | $2 | ... | $4 | category_y


    The table is as follows:
    tbl_document
    Code:
    doc_num | year | amt1 | amt2 | ... | amtn6 | type

    So my query is doing the following:


    Filter out the following:
    financial records with min(fy) <= 2016
    records of a specific type (let's call it category_x); type <> category_x
    closed records; amt1 = amt2 and amt2 = amt3 and ... amtn-1 = amtn


    I need to generate the following query for records that aren't filtered:
    Code:
        document_num | min(fy) | sum(amt1) | sum(amt2) | sum(amt3)

    Here are the queries I am using:


    qry_cat_x_account:
    Code:
    SELECT doc_num, year, FROM tbl_document WHERE (type = "cat_x");

    qry_closed_document:
    Code:
    SELECT doc_num, min(year) As min_year 
    FROM tbl_document AS A 
    GROUP BY doc_num
    HAVING ((A.amt1 = A.amt2) AND (A.amt2 = A.amt3) AND (A.amt3 = A.amt4) AND (A.amt4 = A.amt5) AND (A.amt5=A.amt6));

    qry_ineligible_document:
    Code:
    SELECT doc_num, min(year) AS min_year FROM (select doc_num, fy FROm qry_cat_x_account UNION select grant_doc_nr, fy FROM qry_closed_document) GROUP BY doc_num;

    qry_compiled_eligible_document:
    Code:
    SELECT doc_num, sum(amt1) As sum_amt1, sum(amt2) As sum_amt2, sum(amt3) As sum_amt3, min(year) As min_year FROM tbl_document
    WHERE doc_num NOT IN (select doc_num FROM quni_ineligible_document)
    GROUP BY doc_num
    HAVING min(year)>=16;

    EDIT: after doing some research on indexes, I decided to implemented a composite unique index on doc_num, year as well as a singular index on both year and on type. The speed is still very slow. The individual queries are all instantaneous, but qry_compiled_eligible_document that utilizes them all is horrendously slow. I'm starting to think that utilizing these pre-built queries do not benefit from indexing after they are initially built.


    EDIT2: After fiddling around for a while, I rewrote the entire query as follows and it's lightning fast now, all I did was rewrite it such that it no longer depended on pre-built queries:


    Code:
    SELECT t.doc_num, sum(t.amt1) As sum_amt1, sum(t.amt2) As sum_amt2, sum(t.amt3) As sum_amt3, min(t.year) As min_year FROM tbl_document As t 
    GROUP BY t.doc_num 
    HAVING min(t.year)>=16 AND NOT 
    ((sum(t.amt1) = sum(t.amt2)) AND (sum(t.amt2) = sum(t.amt3)) AND (sum(t.amt3) = sum(t.amt4)) AND (sum(t.amt4) = sum(t.amt5)) AND (sum(t.amt5)=sum(t.amt6)));
    Last edited by TerraEarth; 05-13-2019 at 05:48 PM.

  4. #4
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Be aware that "Year" and "Type" are reserved words in Access and shouldn't be used as object names.

    So a sample might look like the following:


    doc_num | year | amt1 | amt2 | ... | amt6 | type
    doc1 | 2016 | $1 | $1 | ... | $2 | category_x
    doc1 | 2017 | $1 | $1 | ... | $1 | category_x
    doc2 | 2017 | $2 | $2 | ... | $4 | category_y
    "Year" is a built in function.
    Plus, "Year" and "Type" are not very descriptive names..
    "Year" of what???
    "Type" of what??

    Maybe use "fYear" or "finYear".
    Maybe use "CategoryType" or "CatType"...



    The Access Gnomes can sometimes get very obstinate/persnickety when reserved words are used for object names.


    Here is a list of reserved words:
    http://allenbrowne.com/AppIssueBadWord.html

  5. #5
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    I have an extended article on optimising queries that you may find useful http://www.mendipdatasystems.co.uk/s...s-8/4594556613
    This includes an example database that you can easily adapt for your own tests.
    Hope that helps.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Improving Speed on a Top N query
    By orange in forum Tutorials
    Replies: 2
    Last Post: 01-05-2015, 01:42 PM
  2. How to speed up my Query
    By Cfish3r in forum Queries
    Replies: 2
    Last Post: 10-18-2012, 02:55 AM
  3. maybe some tips on a .csv query?
    By dr_patso in forum Access
    Replies: 3
    Last Post: 07-13-2011, 12:08 PM
  4. Improve query speed
    By FMJO in forum Queries
    Replies: 3
    Last Post: 02-10-2011, 08:37 AM
  5. Forecast Query with stepwise increasing year
    By theracer06 in forum Queries
    Replies: 1
    Last Post: 08-12-2010, 06:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums