[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Need to speed up a SQL statement...

Posted on 2007-07-23
Medium Priority
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

ghp7000 earned 800 total points
ID: 19547562
create an index on uid, version, do the runstats, re run the query

Author Comment

ID: 19547630


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 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 400 total points
ID: 19547933
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,

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

LVL 18

Assisted Solution

by:Dave Ford
Dave Ford earned 400 total points
ID: 19548197


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 46

Expert Comment

by:Kent Olsen
ID: 19548269
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

momi_sabag earned 400 total points
ID: 19548362

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

ID: 31407527

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

834 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