![]() |
|
|
#1 |
|
Junior Member
Join Date: May 2005
Posts: 9
|
Query optimisation
I have inherited a fairly clunky database design for the company I work at. This query I'm running takes nearly 3 minutes to run, so I daren't un it on the website!
What I need to do is delete items from the products table where that item does not appear in an order. I was starting off with just selecting the items concerned: Code:
Could anyone suggest a way to run the query without killing the server? I know left joins are slow but I couldnt think of another way to do it. BTW I'm stuck in the dark ages running 4.0. Here's the definition: Code:
Last edited by hessodreamy : 10-10-2005 at 12:01 PM. |
|
|
|
|
|
#2 | |
|
Senior Member
Join Date: Jul 2004
Posts: 167
|
Re: Query optimisation
Quote:
How many records in each database? Have you tried formulating the data in different ways and running EXPLAIN on them? The best way to improve your queries is running EXPLAIN, learning what columns are used to select the data. Using the most efficient way possible once you see how many records had to be sorted depending on your wording of the query. And adding indexes where appropriate then rerunning the EXPLAIN. The above is just some general rules. In your case, sounds like an index on supplierID will do wonders. Try it and report back.
__________________
** Most misspellings intentional to combat spaham filterz** |
|
|
|
|
|
|
#3 |
|
Junior Member
Join Date: May 2005
Posts: 9
|
Re: Query optimisation
Ah yes. Indexes on tProducts.supplierId and tOrderProds.productID worked a treat. I was wary of adding extra indexes that wouldn't often be used, as I understood that too many indexes were as bad as not enough, but that's a 'discuss...' type issue I'll go read up on. Thanks for the help.
|
|
|
|
|
|
#4 | |
|
Senior Member
Join Date: Jul 2004
Posts: 167
|
Re: Query optimisation
Quote:
Not enough indexes slows you down when you are doing a SELECT and need an index. Too many indexes slows you down a bit when you are inserting a new row in the database. If you have an application that has to be fast, inserts many new rows simulateneously and only rarely does a SELECT, than many indexes can be problematic. If on the other hand you rarely add a new row but SELECT all the time, indexes will be barely be felt. Also a rule of thumb for indexes. You want to use them on columns in the WHERE field of your common selects. Use EXPLAIN on your query to find out if your index is being used where you think it is.
__________________
** Most misspellings intentional to combat spaham filterz** |
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Will Paypal you $20 to write this one line select query | Synpax | General | 4 | 02-16-2005 07:58 PM |
| MS Access pass through query - ODBC error | Snowsride | SQL syntax | 0 | 12-22-2004 10:04 AM |
| Help - SELECT COUNT of a UNION Query | Vmusic | SQL syntax | 0 | 12-16-2004 10:21 AM |
| Creating a conditional query | gcornish | SQL syntax | 8 | 08-16-2004 12:24 AM |
| Using one query to search from two tables | Justice | SQL syntax | 1 | 06-25-2004 12:34 PM |