![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
If I have the following: CREATE TABLE ir_sample_table ( * * * * first_id * * * * INT * * * * * * * NOT NULL, * * * * second_id * * * *INT * * * * * * * NOT NULL, * * * * thrid_id * * * * INT * * * * * * * NOT NULL, * * * * some_date * * * *DATETIME * * * * *NOT NULL, * * * * junk * * * * * * VARCHAR(25) * * * * * NULL, * * * * junk_2 * * * * * VARCHAR(25) * * * * * NULL ) LOCK datapages ON 'default' partition BY roundrobin 1 GO EXEC sp_primarykey 'ir_sample_table', 'first_id', 'second_id', 'third_id', 'some_date' GO CREATE UNIQUE CLUSTERED INDEX ir_sample_table_uc_idx_1 ON ir_ir_sample_table ( * some_date, * first_id, * second_id, * thrid_id ) GO I thought sp_primarykey could create a unique clustered index? But, I can run both the commands and they work. If I use sp_primarykey then it's just creating a constraint like a foreign key enforcing referential integrity? It's not creating a unique clustered index? |
#3
| |||
| |||
|
|
I thought sp_primarykey [did] create a unique clustered index? |
|
But, I can run both the commands and they work. If I use sp_primarykey then it's just creating a constraint like a foreign key enforcing referential integrity? |
|
It's not creating a unique clustered index? |
#4
| |||
| |||
|
|
On Oct 19, 11:44*am, 1dg618 <1dg... (AT) gmail (DOT) com> wrote: I thought sp_primarykey [did] create a unique clustered index? No But, I can run both the commands and they work. If I use sp_primarykey then it's just creating a constraint like a foreign key enforcing referential integrity? No. It's not creating a unique clustered index? No. sp_primarykey, sp_commonkey and sp_foreignkey are relics from the old days, before SQL had the syntax to implement indices via PRIMARY KEY, UNIQUE and FOREIGN KEY CONSTRAINTS. They were just declarations that were placed in the catalogue, which report tools etc could pick up and use. *Nowadays, they read the CONSTRAINTS from the catalogue, and the declarations are not relevant. *They create no indices or CONSTRAINTS. UNIQUE [CLUSTERED] and PRIMARY KEY [CLUSTERED] CONSTRAINTS do create indices. CREATE [UNIQUE] [CLUSTERED] INDEX creates an index. You need to be careful not to accidentally create duplicate indices, either by mixing CONSTRAINTS with CREATE INDEX statements, or by CREATE INDEX with the same column group, but with another index_name. Regards Derek |
#5
| |||
| |||
|
|
On Oct 19, 11:44*am, 1dg618 <1dg... (AT) gmail (DOT) com> wrote: I thought sp_primarykey [did] create a unique clustered index? No But, I can run both the commands and they work. If I use sp_primarykey then it's just creating a constraint like a foreign key enforcing referential integrity? No. It's not creating a unique clustered index? No. sp_primarykey, sp_commonkey and sp_foreignkey are relics from the old days, before SQL had the syntax to implement indices via PRIMARY KEY, UNIQUE and FOREIGN KEY CONSTRAINTS. They were just declarations that were placed in the catalogue, which report tools etc could pick up and use. *Nowadays, they read the CONSTRAINTS from the catalogue, and the declarations are not relevant. *They create no indices or CONSTRAINTS. UNIQUE [CLUSTERED] and PRIMARY KEY [CLUSTERED] CONSTRAINTS do create indices. CREATE [UNIQUE] [CLUSTERED] INDEX creates an index. You need to be careful not to accidentally create duplicate indices, either by mixing CONSTRAINTS with CREATE INDEX statements, or by CREATE INDEX with the same column group, but with another index_name. Regards Derek |
#6
| |||
| |||
|
|
On Oct 19, 2:01*pm, Derek Asirvadem <derek.asirva... (AT) gmail (DOT) com> wrote: On Oct 19, 11:44*am, 1dg618 <1dg... (AT) gmail (DOT) com> wrote: I thought sp_primarykey [did] create a unique clustered index? No But, I can run both the commands and they work. If I use sp_primarykey then it's just creating a constraint like a foreign key enforcing referential integrity? No. It's not creating a unique clustered index? No. sp_primarykey, sp_commonkey and sp_foreignkey are relics from the old days, before SQL had the syntax to implement indices via PRIMARY KEY, UNIQUE and FOREIGN KEY CONSTRAINTS. They were just declarations that were placed in the catalogue, which report tools etc could pick up and use. *Nowadays, they read the CONSTRAINTS from the catalogue, and the declarations are not relevant. *They create no indices or CONSTRAINTS.. UNIQUE [CLUSTERED] and PRIMARY KEY [CLUSTERED] CONSTRAINTS do create indices. CREATE [UNIQUE] [CLUSTERED] INDEX creates an index. You need to be careful not to accidentally create duplicate indices, either by mixing CONSTRAINTS with CREATE INDEX statements, or by CREATE INDEX with the same column group, but with another index_name. Regards Derek Date Author Book isin junk1 junk2 junk3 primary key clustered index: Date, Author, Book A clustered index is a good idea for example when a date is involved? For example, if I have 29 million records going back to 1980 and I want to find a book from 1997 by a particular author a clustered index with the date first will quickly get to the 1997 records then quickly to the author then it might have to scan for the book? I think, the primary key clustered index will find the record pretty quick? Now, if I want to find the isin and I know a date range the primary key clustered index will quickly narrow the records because of the date but may have to scan for the isin in that date range? Is there a way to prevent a table scan for the isin? or a way to quickly find the isin? Would a nonclustered index on isin help a query? In plans, I see queries doing forward scans and table scans. I assume, a forward scan uses an index to drill down to the records then table scans for the records? I know I'm asking a lot of questions, but this is pretty interesting stuff. There's a ton of informaton out there, but it's not really straightforward. |
#7
| |||
| |||
|
|
On Oct 19, 3:09*pm, 1dg618 <1dg... (AT) gmail (DOT) com> wrote: On Oct 19, 2:01*pm, Derek Asirvadem <derek.asirva... (AT) gmail (DOT) com> wrote: On Oct 19, 11:44*am, 1dg618 <1dg... (AT) gmail (DOT) com> wrote: I thought sp_primarykey [did] create a unique clustered index? No But, I can run both the commands and they work. If I use sp_primarykey then it's just creating a constraint like a foreign key enforcing referential integrity? No. It's not creating a unique clustered index? No. sp_primarykey, sp_commonkey and sp_foreignkey are relics from the old days, before SQL had the syntax to implement indices via PRIMARY KEY, UNIQUE and FOREIGN KEY CONSTRAINTS. They were just declarations that were placed in the catalogue, which report tools etc could pick up and use. *Nowadays, they read the CONSTRAINTS from the catalogue, and the declarations are not relevant. *They create no indices or CONSTRAINTS. UNIQUE [CLUSTERED] and PRIMARY KEY [CLUSTERED] CONSTRAINTS do create indices. CREATE [UNIQUE] [CLUSTERED] INDEX creates an index. You need to be careful not to accidentally create duplicate indices, either by mixing CONSTRAINTS with CREATE INDEX statements, or by CREATE INDEX with the same column group, but with another index_name. Regards Derek Date Author Book isin junk1 junk2 junk3 primary key clustered index: Date, Author, Book A clustered index is a good idea for example when a date is involved? For example, if I have 29 million records going back to 1980 and I want to find a book from 1997 by a particular author a clustered index with the date first will quickly get to the 1997 records then quickly to the author then it might have to scan for the book? I think, the primary key clustered index will find the record pretty quick? Now, if I want to find the isin and I know a date range the primary key clustered index will quickly narrow the records because of the date but may have to scan for the isin in that date range? Is there a way to prevent a table scan for the isin? or a way to quickly find the isin? Would a nonclustered index on isin help a query? In plans, I see queries doing forward scans and table scans. I assume, a forward scan uses an index to drill down to the records then table scans for the records? I know I'm asking a lot of questions, but this is pretty interesting stuff. There's a ton of informaton out there, but it's not really straightforward. A clustered index is a good idea when you will have a lot of queries involving a range of data *( <, >, between x and y) To find a book by a particular author published in a particular year, an index on author is likely to be far better. *Most authors publish very few book in their lifetime, but large numbers of books are published each year. If all the columns needed to find the isin are keys of an index, ASE can get the isin from the leaf level of the index and avoid reading any of the actual data pages. *This is called a "covered index scan" forward and backward refer just to the direction of the scan. Both table scans and index scans can be done forward or backward. *Backward scans are unusual, usually done due to an ORDER BY ... DESC. *The backward scan returns values in descending order, so eliminates the need to sort the results in a worktable. *However, backward scans increase the odds of deadlocks.- Hide quoted text - - Show quoted text - |
#8
| ||||||||
| ||||||||
|
|
Date Author Book -- Title Sex |
|
junk1 junk2 junk3 PRIMARY KEY CLUSTERED (Date, Author, Book) |
|
A clustered index is a good idea for example when a date is involved? |
|
For example, if I have 29 million records going back to 1980 and I want to find a book from 1997 by a particular author a clustered index with the date first will quickly get to the 1997 records then quickly to the author then it might have to scan for the book? I think, the primary key clustered index will find the record pretty quick? |
|
Now, if I want to find the isin and I know a date range the primary key clustered index will quickly narrow the records because of the date but may have to scan for the isin in that date range? Is there a way to prevent a table scan for the isin? |
|
or a way to quickly find the isin? Would a nonclustered index on isin help a query? |
|
In plans, I see queries doing forward scans and table scans. I assume, a forward scan uses an index to drill down to the records then table scans for the records? |
|
I know I'm asking a lot of questions, but this is pretty interesting stuff. There's a ton of informaton out there, but it's not really straightforward. |
#9
| ||||||
| ||||||
|
|
Makes sense. The primary key clustered index enforces the primary key. I guess, a unique clustered index or a unique non-clustered index would enforce the primary key too? I always put the primary key clustered index on the primary key and ordered the columns from distinct to least distinct... if that makes sense (I don't know another way to say it?). |
|
For example, if my primary key had a date in it, and users would do something like WHERE date <= ? and date >= ? it makes sense to have the date first in the index followed by the rest of the columns. |
|
The idea is to drill down into the records? On the other hand it all depends. In the example above, the author would be first in the index then the date. |
|
Once one decides on the primary key, how should the columns *in a clustered index be ordered? |
|
How should the columns in a non-clustered index be ordered? |
|
Does the order of the columns in the WHERE clause matter? If my unique clustered index is the following: date, first_id, and second_id should my where clause be WHERE date = ? and first_id = ? and second_id = ? or it doesn't matter? If I have WHERE first_id = ? and second_id = ? and date = ? the optimizer will figure it out? |
#10
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |