Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 536
  • Last Modified:

Interbase optimizing SQL Query

I use Interbase 6.01 and Firebird
and have following sql code

What indexes do i need for otimizing this code ?
I have over a half million of rows in this table ?

Is it better to use one index with many columns or generate for every column an individual index ? Whats better for speed and size ?

'SELECT A.ID, A.AUFTRAGID, A.TEILID, A.DATUM, A.PARENTID, A.DAUER, '+
        ' M.PARENTID AS MASCHINE '+
        ' FROM AUFTRAG_KOSTEN A '+
        ' LEFT OUTER JOIN AUFTRAG_KOSTEN M ON M.ID = A.OTHER_ID '+
        ' WHERE A.ART = 0 AND A.DATUM >= :VON AND A.DATUM < :BIS ORDER BY A.PARENTID, A.DATUM'
0
Dev6
Asked:
Dev6
3 Solutions
 
thegroupCommented:
if you generate several index they can be freely combined to achieve the best results.

the recomended candidates to index are condition fields (where...) and join fields (they are conditions too)

perhaps you don't need to index all of them but only those with more records.

(an active index slow down insertions and deletes and updates, be careful)
0
 
YodaMageCommented:
case 1 : Create Index Index1 On table1 (field1, field2, field3)

case 2 :   Create Index Index1 On table1 (field1)
            Create Index Index2 On table1 (field2)
            Create Index Index3 On table1 (field3)

Case 1 will be more efficient *if* you only
ever do queries which reference all three columns in the WHERE clause. Otherwise choose case 2.

0
 
Dev6Author Commented:
In this case, whch coloumns should i use ?
ART      (in Where)
DATUM    (in Where and Order)
OTHER_ID (in JOIN)     ?????
PARETID  (in Order)    ?????
0
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

 
YodaMageCommented:
In your case I would think an index of (PARETID, DATUM, ART), though to be sure, try that index, then time query, then do it as 3 seperate indexes, and again time query.  
0
 
grolschisgoodCommented:
A.OTHER_ID
A.ART
A.DATUM

you will need indexes on the above fields that are used int he join and where clause.  An index on fields int he order cluase will be off no benefit.

Also do not index any of the above fields if they contain a small set of distinct values.  eg if A.ART only contain values 0 and 1 then an index on this field will be off no use.

You also do not need to index primary key fields as they already have an index.  therefore you should not need an index on  M.ID as this appears to be a primary key.

you should create a seperate index for each field for maximum flexibility, however you could create a single index for A.OTHER_ID, and a single index containing both A.ART and A.DATUM. (rem exclude A.ART if it does not have many distinct values)

Also note the more indexes you have the slower it is to INSERT data into the table, so dont go crazy on indexes that are not needed.

k
cam
0
 
kacorretiredCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
       to split points as follows:
                 66 points for thegroup
                 66 points for grolschisgood
                 67 points for YodaMage
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

kacor
EE Cleanup Volunteer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now