 |
|
 |
|
Next: Database Design/Combo Boxes and Relationships
|
| Author |
Message |
External

Since: Mar 19, 2007 Posts: 550
|
(Msg. 1) Posted: Tue Jun 24, 2008 6:18 pm
Post subject: One example of a slow query. Archived from groups: comp>databases (more info?)
|
|
|
At one site, I managed to verify that one of these two queries ran 10 to
100 times faster than the other:
select * from customers
where country = 'US' and
state = 'TX' and
City = 'Dallas'
select * from customers
where state = 'TX' and
City = 'Dallas'
I expected the first one to run much faster and it did. Why? Because
there was an index with a compound index key, namely country, state, and
city. In the second case the poor optimizer was faced with walking the
index instead of a quick lookup, as it could do in the first case.
But the clever programmers had used the second form, because they knew that
all the customers were in US, and they wanted to keep things simple for the
DBMS.
Not only were they running way too slow, but also, they had written code
that could break when the user community decided to extend the customer base
to other countries.
This is just one example out of hundreds of possible examples, where the
programmers code strangely in order to speed things up, and instead they
slow things down. >> Stay informed about: One example of a slow query. |
|
| Back to top |
|
 |  |
External

Since: Jun 29, 2006 Posts: 38
|
(Msg. 2) Posted: Wed Jun 25, 2008 9:13 am
Post subject: Re: One example of a slow query. [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
External

Since: Apr 20, 2007 Posts: 83
|
(Msg. 3) Posted: Wed Jun 25, 2008 9:13 am
Post subject: Re: One example of a slow query. [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Jun 25, 4:13 am, David Segall wrote:
> "David Cressey" wrote:
> > Because
> >there was an index with a compound index key, namely country, state, and
> >city.
>
> Why would you do that instead of indexing the fields separately?
Better is one index defines as city, state, country.
Put the most discriminating element first. then partial searches on
that index still work.
Ed >> Stay informed about: One example of a slow query. |
|
| Back to top |
|
 |  |
External

Since: Apr 20, 2007 Posts: 83
|
(Msg. 4) Posted: Wed Jun 25, 2008 9:13 am
Post subject: Re: One example of a slow query. [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Jun 25, 6:55 am, "Arved Sandstrom"
wrote:
> "Roy Hann" wrote in message
>
>
>
>
>
> > "David Segall" wrote in message
> >
> >> "David Cressey" wrote:
>
> >>> Because
> >>>there was an index with a compound index key, namely country, state, and
> >>>city.
> >> Why would you do that instead of indexing the fields separately?
>
> > Most DBMS engines will use only one index for a particular restriction
> > step, so in general you will want to define compound keys where they make
> > sense.
>
> > Depending on the implementation of the index it might be possible to
> > exploit right-incomplete key values, so you would try to define your key
> > with the most frequently known parts of the key are on the left. Then
> > although the key may include more columns than you can supply a value for,
> > you might still be able to exploit it if you supply the parts you do know.
>
> > Roy
>
> How common is the latter implementation? MySQL evidently does this, so that
> if you had an index on (col1,col2,col3), queries on (col1), (col1,col2) and
> (col1,col2,col3) all use the index, but a query on (col2,col3), for example,
> does not. What is this incomplete use of multicolumn indexes called,
> exactly?
Oracle calls it a range scan in the EXPLAIN PLAN report. And that
makes sense if you think about it. (at least it does to me.)
> ... I ran across articles that referred to this as partial indexing,
> but my understanding of a partial (filtered) index is that the latter
> indexes on a subset of rows, which is something different. I'm guessing that
> for any given DBMS you'd just have to look up the docs on multicolumn
> indexing and see what they say about using leading columns.
>
> As I understand it, it wouldn't be totally useless to have only the separate
> indexes for the three columns, namely (col1), (col2), and (col3), as the
> DBMS might use index combination (as does PostgreSQL 8.1 and up). Or perhaps
> even better, have the multicolumn index and one or more single-column
> indexes, depending on what you anticipate the most common queries to be.
>
> AHS
If you must have one index, put the lower unit element first. So on a
phone number table
you might put Number, Exchange, Area code (e.g. 800-123-4567 indexed
as 4567,123,800). then partial searches on Exchange and number can use
the index.
Ed >> Stay informed about: One example of a slow query. |
|
| Back to top |
|
 |  |
External

Since: Oct 20, 2004 Posts: 472
|
(Msg. 5) Posted: Wed Jun 25, 2008 11:12 am
Post subject: Re: One example of a slow query. [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"David Segall" wrote in message
> "David Cressey" wrote:
>
>> Because
>>there was an index with a compound index key, namely country, state, and
>>city.
> Why would you do that instead of indexing the fields separately?
Most DBMS engines will use only one index for a particular restriction step,
so in general you will want to define compound keys where they make sense.
Depending on the implementation of the index it might be possible to exploit
right-incomplete key values, so you would try to define your key with the
most frequently known parts of the key are on the left. Then although the
key may include more columns than you can supply a value for, you might
still be able to exploit it if you supply the parts you do know.
Roy >> Stay informed about: One example of a slow query. |
|
| Back to top |
|
 |  |
External

Since: Jun 25, 2008 Posts: 1
|
(Msg. 6) Posted: Wed Jun 25, 2008 11:55 am
Post subject: Re: One example of a slow query. [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"Roy Hann" wrote in message
> "David Segall" wrote in message
>
>> "David Cressey" wrote:
>>
>>> Because
>>>there was an index with a compound index key, namely country, state, and
>>>city.
>> Why would you do that instead of indexing the fields separately?
>
> Most DBMS engines will use only one index for a particular restriction
> step, so in general you will want to define compound keys where they make
> sense.
>
> Depending on the implementation of the index it might be possible to
> exploit right-incomplete key values, so you would try to define your key
> with the most frequently known parts of the key are on the left. Then
> although the key may include more columns than you can supply a value for,
> you might still be able to exploit it if you supply the parts you do know.
>
> Roy
How common is the latter implementation? MySQL evidently does this, so that
if you had an index on (col1,col2,col3), queries on (col1), (col1,col2) and
(col1,col2,col3) all use the index, but a query on (col2,col3), for example,
does not. What is this incomplete use of multicolumn indexes called,
exactly? I ran across articles that referred to this as partial indexing,
but my understanding of a partial (filtered) index is that the latter
indexes on a subset of rows, which is something different. I'm guessing that
for any given DBMS you'd just have to look up the docs on multicolumn
indexing and see what they say about using leading columns.
As I understand it, it wouldn't be totally useless to have only the separate
indexes for the three columns, namely (col1), (col2), and (col3), as the
DBMS might use index combination (as does PostgreSQL 8.1 and up). Or perhaps
even better, have the multicolumn index and one or more single-column
indexes, depending on what you anticipate the most common queries to be.
AHS >> Stay informed about: One example of a slow query. |
|
| Back to top |
|
 |  |
External

Since: Mar 19, 2007 Posts: 550
|
(Msg. 7) Posted: Wed Jun 25, 2008 2:10 pm
Post subject: Re: One example of a slow query. [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"Roy Hann" wrote in message
> "David Segall" wrote in message
>
> > "David Cressey" wrote:
> >
> >> Because
> >>there was an index with a compound index key, namely country, state,
and
> >>city.
> > Why would you do that instead of indexing the fields separately?
>
> Most DBMS engines will use only one index for a particular restriction
step,
> so in general you will want to define compound keys where they make sense.
>
In general, true. However, the optimizer in the DBMS in this case would
have been capable of exploiting separate simple indexes. The question
remians, why would the index designer have chosen a single index with a
compound key over multiple single indexes.
Several possible reasons:
Query speed. For the query that includes country, state, and city, the
single index can locate the right rows with fewer logical disk reads, and
therefore fewer physical disk reads.
Update speed. It takes fewer disk writes to commit a new row with one
compound index than with multiple simple indexes.
Psychological factors. Just as designers tend to think of a schema with
fewer tables as "simpler" than than one with more tables, so likewise they
might think of a table with fewer indexes as "simpler".
Why didn't I change the index design? I was a visiting instructor for one
week. I didn't know what the other consequences of changing the index
design would be. The original designer may have had considerations that
eluded me. Index design involves anticipating traffic. There's always a
little guesswork in it.
Besides, I was teaching programmers tuning, and this gave me an excellent
opportunity to teach a key point: that a query that provides more selection
criteria can run a lot faster than one with fewer criteria. This is counter
intuitive to programmers. And a real live case from their production
database was worth more than a score of prepared cases from my course
materials.
> Depending on the implementation of the index it might be possible to
exploit
> right-incomplete key values, so you would try to define your key with the
> most frequently known parts of the key are on the left. Then although the
> key may include more columns than you can supply a value for, you might
> still be able to exploit it if you supply the parts you do know.
>
This is absolutely correct. But, for reasons that I can't really fathom,
people tend to design
compound indexes in top down order: country, state, city. I imagine that
city, state, country would almost always be more useful. But both
programmers and designers have been trained not to ever think bottom up, so
they reject a bottom up index without careful analysis. This is just my
take on it.
However, the top down index might be more useful in a large reporting query,
where the report is being grouped by country, state, city. There are a lot
of things to consider when doing index design. >> Stay informed about: One example of a slow query. |
|
| Back to top |
|
 |  |
External

Since: Mar 19, 2007 Posts: 550
|
(Msg. 8) Posted: Wed Jun 25, 2008 2:13 pm
Post subject: Re: One example of a slow query. [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"Arved Sandstrom" wrote in message
> "Roy Hann" wrote in message
>
> > "David Segall" wrote in message
> >
> >> "David Cressey" wrote:
> >>
> >>> Because
> >>>there was an index with a compound index key, namely country, state,
and
> >>>city.
> >> Why would you do that instead of indexing the fields separately?
> >
> > Most DBMS engines will use only one index for a particular restriction
> > step, so in general you will want to define compound keys where they
make
> > sense.
> >
> > Depending on the implementation of the index it might be possible to
> > exploit right-incomplete key values, so you would try to define your key
> > with the most frequently known parts of the key are on the left. Then
> > although the key may include more columns than you can supply a value
for,
> > you might still be able to exploit it if you supply the parts you do
know.
> >
> > Roy
>
> How common is the latter implementation? MySQL evidently does this, so
that
> if you had an index on (col1,col2,col3), queries on (col1), (col1,col2)
and
> (col1,col2,col3) all use the index, but a query on (col2,col3), for
example,
> does not. What is this incomplete use of multicolumn indexes called,
> exactly? I ran across articles that referred to this as partial indexing,
> but my understanding of a partial (filtered) index is that the latter
> indexes on a subset of rows, which is something different. I'm guessing
that
> for any given DBMS you'd just have to look up the docs on multicolumn
> indexing and see what they say about using leading columns.
The term I've seen in the literature is "range retrieval". Range retrieval
also describes
criteria like "where salary between 50000 and 100000".
>
> As I understand it, it wouldn't be totally useless to have only the
separate
> indexes for the three columns, namely (col1), (col2), and (col3), as the
> DBMS might use index combination (as does PostgreSQL 8.1 and up). Or
perhaps
> even better, have the multicolumn index and one or more single-column
> indexes, depending on what you anticipate the most common queries to be.
>
Yes but... the more indexes the slower inserts will run. There are multiple
tradeoffs here. >> Stay informed about: One example of a slow query. |
|
| Back to top |
|
 |  |
External

Since: Oct 31, 2007 Posts: 6
|
(Msg. 9) Posted: Wed Jun 25, 2008 2:16 pm
Post subject: Re: One example of a slow query. [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Arved Sandstrom, 25.06.2008 13:55:
> How common is the latter implementation? MySQL evidently does this, so that
> if you had an index on (col1,col2,col3), queries on (col1), (col1,col2) and
> (col1,col2,col3) all use the index, but a query on (col2,col3), for example,
> does not. What is this incomplete use of multicolumn indexes called,
> exactly?
I think that this is related to the fact that most of the time the index is implemented as a B-Tree index. And technically it is not possible to access the "second level" directly without finding the "first level".
> As I understand it, it wouldn't be totally useless to have only the separate
> indexes for the three columns, namely (col1), (col2), and (col3), as the
> DBMS might use index combination (as does PostgreSQL 8.1 and up). Or perhaps
> even better, have the multicolumn index and one or more single-column
> indexes, depending on what you anticipate the most common queries to be.
Yes and no. Keep in mind that each index will add an overhead when updating, deleting or inserting rows. So if you have a lot of indexes, your update performance may degrade. The "art" is finding the proper balance between the two requirements (fast reads and fast writes)
Postgres does a indeed good job in "re-using" indexes. For e.g. Oracle something similar could be achieved using bitmap indexes (but they have higher update costs and are not always feasible)
Thomas >> Stay informed about: One example of a slow query. |
|
| Back to top |
|
 |  |
| Related Topics: | Please HELP. PostgreSQL running so slow ... - I have a fast P4 Linux server with 2GB mem and yet running a simply SELECT such as: select part_number FROM catalog WHERE part_number LIKE 'HH%' on a catalog table with 14,000 records takes 41.57 seconds. Can anyone help ... PLEASE ... Thank you for...
need help with query - All, Can you folks save me some time and headaches? I have a table with information on each individual in my business. (PKID; Job Title; FName; LName; etc). I have a seperate table for each evaluator (who holds a higher position). (DUPLICATE..
LIttle query help - I am potentially building a music listening site and want the users to be able to place songs into their favorites list. Easy enough, stick the id of the song, from the songs table, into the users favorites. From this however, how would I go about..
Help with SQL query.... I'm sure there is a way! - Hi, I'm helping a friend to build a system to handle advertisments from local dealers in different regions/cities. In this system, an advertisment belongs to a region and is connected to one or more categories (in this context called searchwords). One....
SQL Query - A better way? - I have a query that I have written and it works, but it seems a bit redundant and I am wondering if there is a better way to write it. My basic problem is I want to pull only 1 record, and that record is the newest one based on the LASTUPDATE date field... |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|