Re: composite key question
From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 09/18/04
- Next message: Hugo Kornelis: "Re: URGENT: using update to update more then 1 table"
- Previous message: Jonah Olsson: "Re: Trying to implent (Joe Celko's) Nested Sets, but need more data columns"
- In reply to: Stefan Berglund: "Re: composite key question"
- Next in thread: Robert Bouillon: "Re: composite key question"
- Reply: Robert Bouillon: "Re: composite key question"
- Reply: Stefan Berglund: "Re: composite key question"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 18 Sep 2004 22:33:02 +0200
On Sat, 18 Sep 2004 10:45:21 -0700, Stefan Berglund wrote:
>On Thu, 16 Sep 2004 23:09:25 +0200, Hugo Kornelis
><hugo@pe_NO_rFact.in_SPAM_fo> wrote:
(snip)
>>You don't need an extra index, but you do need to redefine your existing
>>index: instead of
>> PRIMARY KEY (id, library_id)
>>it should read
>> PRIMARY KEY (library_id, id)
>>
>>This way, the index associated with the primary key will be suitable for
>>finding the maximum id for a specific library_id (1 in your example
>>query).
(snip)
>Hugo, would you mind elaborating on this or pointing me to an
>article that explains why this is desirable. I don't understand
>why you state that "the index associated with the primary key
>will be suitable for finding the maximum id for a specific
>library_id". Does it have to do with the magnitude of the
>numbers involved or something else entirely?
Hi Stefan,
Something else entirely. I'll use an analogy to explain.
Imagine I give you the phone book of the Dutch city Amsterdam. All entries
in this phone book are ordered by last name; people with the same surname
are ordered by streetname.
If I'd ask you for the alphabetically last street name in Amsterdam where
someone named "Jansen" lives, you'd quickly find the "J" entries, skip to
a page containing entries for "Jansen", find the last entry and tell me
the street name of this entry. Should be possible in less than a minute.
Now I'm gonna ask you for the alphabetically last surname of all people
living in the Kalverstraat (a street in Amsterdam). What can you do? The
ordering applied to the entries in the phone book won't help you. The best
way to do this is to start at the last page and read back until you
encounter an entry with street Kalverstraat - this will be the
alphabetically last person on the Kalverstraat. Will take you several
minutes if you're lucky, hours if you're unlucky.
The same applies to Greg's problem. If the PK is defined (lib_id, id), a
search for the highest id of a given lib_id can be accomplished by
locating an entry with the correct lib_id and using that as a start point
for locating the highest id with that lib_id. But if the PK is defined as
(id, lib_id), all index entries have to be scanned (unless SQL Server is
capable of scanning an index backwards and the optimizer realizes that
this would be a better plan for this query - and even in that case, it
would still be slower).
Best, Hugo
-- (Remove _NO_ and _SPAM_ to get my e-mail address)
- Next message: Hugo Kornelis: "Re: URGENT: using update to update more then 1 table"
- Previous message: Jonah Olsson: "Re: Trying to implent (Joe Celko's) Nested Sets, but need more data columns"
- In reply to: Stefan Berglund: "Re: composite key question"
- Next in thread: Robert Bouillon: "Re: composite key question"
- Reply: Robert Bouillon: "Re: composite key question"
- Reply: Stefan Berglund: "Re: composite key question"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|