Need to speed up a SQL statement...

Posted on 2007-07-23
Last Modified: 2010-04-21
Hello World!

I have a select statement that takes a long time to run because I have loads of data in the table.  It takes easily 10 to 15 minutes to return values...

Here is my select statement. The name of the table is "logs", the timestamp is set to 4 a.m. on Monday (today)...  I'm trying to get the count of items in the table "before" the given timestamp.  Any ideas how I can make this faster?

select my_id, count(distinct uid) from logs where (timestamp('2007-07-16-') - version) >= 0 group by my_id

my_id ==> CHARACTER(4)
uid ==> CHARACTER(20)
version ==> TIMESTAMP

-- Bubba
Question by:bganoush
    LVL 13

    Accepted Solution

    create an index on uid, version, do the runstats, re run the query

    Author Comment



    What do you mean "do the runstats"?

    Also, would it be faster if insted of subtracting the timestamp with version that I just compare the version to my timestamp?

    select my_id, count(distinct uid) from logs where timestamp('2007-07-16-') >= version group by my_id
    LVL 45

    Assisted Solution

    Hi bqanoush,

    I'd write the query in a slightly more understandable form:

      count(distinct uid)
    from logs
    where version <= '2007-07-16-'
    group by my_id;

    But first I'd follow the indexing advise of ghp7000:

      create index myidx000 on log (version, my_id);
      runstats on table log for index myidx000;

    Note that runstats is not handled my most SQL clients.  You'll have to run that from the DB2 command line or a client that knows how to pass it to DB2.

    Good Luck,

    LVL 18

    Assisted Solution



    Kent's index suggestion is absolutely the FIRST thing to try.

    Outside of that, it might depend on which "flavor" of DB2 you're running. (iSeries, Linux/Unix/Windows, zSeries, etc)

    For instance, in DB2 for iSeries, "runstats" doesn't exist. Database statistics are collected and updated automatically.

    You'd use Visual Explain (in the "database" tab of iSeries Navigator) to determine the execution plan of the query.  Most likely, it will indicate that you need to create the index that Kent advised.

    Possibly, though, it may show you that the index exists, but the query can't use it. Sometimes, this happens the sort-sequence of the query is different than the sort-sequence of the tables and indices.

    Start by creating the index and testing it again. If that doesn't work, post back which DB2 you're using, and we can target our comments more effectively.

    LVL 45

    Expert Comment

    Hi Dave,

    I was wondering if any of the flavors of DB2 consider this inline cast to be a function call and void the used of indexes on the column:


    LVL 37

    Assisted Solution


    first of all - the predicate
    where (timestamp('2007-07-16-') - version) >= 0
    is not always indexable (depends on platform and version)
    where version <= '2007-07-16-'
    is always indexable, so i would use the second one (like KDO suggested)

    second of all, i would define an index on (version, my_id,uid)
    this way db2 can scan only the index (no need to access the table itself) and the query will perform faster

    Author Closing Comment


    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
    Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video discusses moving either the default database or any database to a new volume.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now