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

x
?
Solved

Need to speed up a SQL statement...

Posted on 2007-07-23
7
Medium Priority
?
665 Views
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-04.00.00.000000') - version) >= 0 group by my_id

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

-- Bubba
0
Comment
Question by:bganoush
7 Comments
 
LVL 13

Accepted Solution

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

Author Comment

by:bganoush
ID: 19547630

Thanks...

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-04.00.00.000000') >= version group by my_id
0
 
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:

select
  my_id,
  count(distinct uid)
from logs
where version <= '2007-07-16-04.00.00.000000'
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,
Kent

0
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

Bubba,

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.

HTH,
DaveSlash
0
 
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:

  timestamp('2007-07-16-04.00.00.000000')


Kent
0
 
LVL 37

Assisted Solution

by:momi_sabag
momi_sabag earned 400 total points
ID: 19548362
hi

first of all - the predicate
where (timestamp('2007-07-16-04.00.00.000000') - version) >= 0
is not always indexable (depends on platform and version)
but
where version <= '2007-07-16-04.00.00.000000'
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
0
 

Author Closing Comment

by:bganoush
ID: 31407527
Thanks...
0

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