| Author |
Topic |
|
WoodHouse
Posting Yak Master
211 Posts |
Posted - 2010-09-21 : 05:05:19
|
| HiTable2 like master table and total records is 60491Table1 is detail table and total records is 56087No index both the tablesSELECT A.*, b.*FROM dbo.Table1 a LEFT OUTER JOIN dbo.table2 b ON a.Number=b.Number and source = @sourcePlease advice which index is good practice. please advice why we need to useThanks a Lot friends |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-09-21 : 05:58:49
|
| For someone with over 200 posts this is an awful question.Please post the DDL for the tables involved. Depending on the columns and other things different indexes would be recommended.Also -- do both tables have a primary key (existing clustered index) or are they both heaps?Do you need to select EVERYTHING from each table? if you don't then we may recommend an index with an include clause.What version of SQL server are you running?Please post a decent sample query you want to optimise.Or are you telling me that your production code is like that? SELECT A.*, B.*Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
WoodHouse
Posting Yak Master
211 Posts |
Posted - 2010-09-21 : 06:11:12
|
Please post the DDL for the tables involved. Depending on the columns and other things different indexes would be recommended.Also -- do both tables have a primary key (existing clustered index) or are they both heaps?both the tables are heapDo you need to select EVERYTHING from each table? if you don't then we may recommend an index with an include clause.Only required columns like table1.number, table1.source, table2.emp_name, table2.address,table2.cityWhat version of SQL server are you running?SQL SERVER 2005 service pack 2Please post a decent sample query you want to optimise.SELECT a.Number , a.source , a.xyz, a.xyz, b.emp_name, b.emp_address, b.city, b.xyz FROM table1 a LEFT OUTER JOIN dbo.table2 b ON a.Number=b.Number AND a.SOURCE = 'VALUE' Or are you telling me that your production code is like that? SELECT A.*, B.*No only required columns |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-09-21 : 07:16:49
|
| OK. Thanks for that.1) Give the tables a decent primary key / clustered index.The reason for this is that as the tables grow they will just be allocated wherever the engine pleases. Also when querying anything that can't use a complete covering index then you'll have to suffer through RID lookups.You'll have to think about the best column(s) for the CI for each table.Once the clustered index is in place I'd be tempted to go for....2) Some sort of referential integrity.The tables seem to join on the number column? Should this be a foreign key from one table to the other. Should one of the tables maybe have this column as its PRIMARY KEY?Then....3) For Table 1 an Index on number. (is this a good candidate for the primary key? What does this number represent) For this *specific* query then you may also want to cover the SOURCE column4) For Table 2 - probably an index on number.---------------------------------Once that's all been done I'd imagine that you'd get index seek + key lookup (the columns in the SELECT list aren't part of an index) but that's maybe the best you could do. You don't seem sure which columns you need.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
WoodHouse
Posting Yak Master
211 Posts |
Posted - 2010-09-21 : 09:51:39
|
Hi TCThanks a LotShall i create an identity column in Table1 and create clustered index on that column.Please advice is this good practise?Thxquote: Originally posted by Transact Charlie OK. Thanks for that.1) Give the tables a decent primary key / clustered index.The reason for this is that as the tables grow they will just be allocated wherever the engine pleases. Also when querying anything that can't use a complete covering index then you'll have to suffer through RID lookups.You'll have to think about the best column(s) for the CI for each table.Once the clustered index is in place I'd be tempted to go for....2) Some sort of referential integrity.The tables seem to join on the number column? Should this be a foreign key from one table to the other. Should one of the tables maybe have this column as its PRIMARY KEY?Then....3) For Table 1 an Index on number. (is this a good candidate for the primary key? What does this number represent) For this *specific* query then you may also want to cover the SOURCE column4) For Table 2 - probably an index on number.---------------------------------Once that's all been done I'd imagine that you'd get index seek + key lookup (the columns in the SELECT list aren't part of an index) but that's maybe the best you could do. You don't seem sure which columns you need.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
|
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-09-21 : 10:31:15
|
quote: Originally posted by WoodHouse Hi TCThanks a LotShall i create an identity column in Table1 and create clustered index on that column.Please advice is this good practise?Thx
No I'm not suggesting that you just arbitrarily make a new column for your primary key.The primary key should uniquely identify a row in the table. Ideally it should also be something pertinent to the data if possible. An IDENTITY column certainly provides a UNIQUE value for each row but it has nothing to do with the data. That's why it commonly called a SURROGATE primary key if used in that way.PLEASE POST THE DLL of the two tables involved.Do you KNOW what the data looks like in the tables?number seems like it could be a good candidate for a key. However we don't know your data so we can't really advise further.Have you got any experience with database design? You seem to know what the terms are but not why you'd do certain things.I don't mean to offend you but you are asking the sorts of questions that would scare me if one of our developers were asking them.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|