If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > SQL optimizing and indexing problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-28-03, 07:57
zuhara zuhara is offline
Registered User
 
Join Date: Oct 2003
Posts: 3
Question SQL optimizing and indexing problem

I'm having difficulties to optimize my relatively simple query. The query fetches data from one table that have about 0,5 million rows. I'm using EXISTS clause to fetch the wanted rows and have an index created for that, but the query is still quite slow.

Is there a alternative, faster, way to perform the following SQL?

SELECT
m1.document_id, m1.document, m1.attribute
FROM
myTable m1
WHERE
EXISTS ( SELECT * FROM myTable m2 WHERE attribute = 'attrName' AND m1.document_id = m2.document_id )
ORDER BY
m1.document_id, m1.attribute

It seems that the EXISTS clause slows down the query dramatically.
Reply With Quote
  #2 (permalink)  
Old 10-28-03, 10:34
dbmadcap dbmadcap is offline
Registered User
 
Join Date: May 2003
Posts: 87
Re: SQL optimizing and indexing problem

Then try this. If you have proper indexes, it will try to make use of it.

SELECT m1.document_id, m1.document, m1.attribute
FROM myTable m1, myTable m2
WHERE m2.attribute = 'attrName'
AND m1.document_id = m2.document_id
ORDER BY m1.document_id, m1.attribute;

And if you can sacrifice on the order by, do it.

Hope this helps.
Reply With Quote
  #3 (permalink)  
Old 10-29-03, 07:12
zuhara zuhara is offline
Registered User
 
Join Date: Oct 2003
Posts: 3
Smile Re: SQL optimizing and indexing problem

Thanks! The new SQL works fine! It dropped the execution time to 30% and now the clause is even more simpler.

Quote:
Originally posted by dbmadcap
Then try this. If you have proper indexes, it will try to make use of it.

SELECT m1.document_id, m1.document, m1.attribute
FROM myTable m1, myTable m2
WHERE m2.attribute = 'attrName'
AND m1.document_id = m2.document_id
ORDER BY m1.document_id, m1.attribute;

And if you can sacrifice on the order by, do it.

Hope this helps.
Reply With Quote
  #4 (permalink)  
Old 10-29-03, 09:17
Krastio Krastio is offline
Registered User
 
Join Date: Oct 2003
Location: Sofia
Posts: 16
That one script keep original business logic.

SELECT
m1.document_id,
m1.document,
m1.attribute
FROM
myTable m1
WHERE
m1.document_id in (SELECT
document_id
FROM
myTable
WHERE
attribute = 'attrName')
ORDER BY
m1.document_id,
m1.attribute
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On