Showing posts with label selection. Show all posts
Showing posts with label selection. Show all posts

Wednesday, March 21, 2012

indexing question (getting started with index selection)

hi. this may sound super obvious but i am curious about the thought process that goes into index selection for a table. I have designed a new database and have read up on indexes. What i am unclear about is what value to originally assign the index? Say I have 5 tables. Should OneID = 1, TwoID = 2, ThreeID = 3 and so on? what happens when the index of table one and the index of table two are the same values, does that matter? I have run the index selection wizard and used the profiler to create a trace table but, when i am inserting data into my tables, i am asked to insert an index value... i am not sure where i should start/what numbers i should use (without simply putting down something random).

some "getting started" thoughts on indexing would be appreciated.
thanks in advance.I'm not sure what to ask...

huh?

Got any code you can post?|||sorry, i was afraid that would be confusing...
what i am trying to get at is what values do i use as my index id for each table? does it matter what i use. for example, if i have a table:

Create Table Table1
(
TableID
Row1
Row2
Row3
Row4
ForeignKeyID
)

and then i do an first insert:
Insert into table1 (tableid, row1, row2, row3, row4, foreignkeyid)
values (?, value, value, value, value, ?)

what should i be inserting for my Indexes? Right now i am simly assigning random values... 1000 for TableID and 2000 for the foreign keyid, and so on. Will this cause complications as the table grows and numbers overlap, etc?

If this question still sounds awkward, maybe somebody could point me to a good resouce on INdex Selection for new tables? I haven't found anything good online.

thanks again.|||I would make [TableID] an Identity field that would insure a unique value.
And you need to have the value of the foreign key before you insert a new record. Whether it's unique or not it depends on your appliciation (1 to 1 or 1 to many).|||Not clear on what you are asking but maybe these help:

http://www.sql-server-performance.com/gv_index_data_structures.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/createdb/cm_8_des_05_5h6b.asp

http://www.sqlservercentral.com/columnists/chedgate/clusterthatindex.asp

if you read these and still have questions, come on back!!|||it appears that you are asking for a unique key value for each primary key column..
if this is correct you can use the identity function on the column to generate a auto incrementing monotonic key.

identity has 2 arguments the seed and the increment.
seed is the inital start value when the first row is inserted into the table
increment is the step between the previous number and the next number
so an seed of 2000 and an increment of 50 would be
2000
2050
2100
2150
2200

Create table test
(
col1 int Identity(2000,50) not null
)
Hope thi helps.|||I usually use the largest possible negative number for the seed, with an increment of one. I don't consider the possible overlap of ID values to mean diddly squat.

-PatP

Monday, March 12, 2012

Indexing a selection query

Hey,

I've started a new topic for this, as the old was is a different problem, lol.

This script is working now, however it says the unitIndex is 287 for all units.

What I wanted the unitIndex to be is, 1 for the unit with highest tsr, and (if 30 units) 30 for the unit with lowest tsr.

Why is it making the UnitIndex 287, and how can I fix it? lol

(I have marked the line where the unitIndex is

PROCEDURE units_display (@.startIndex INT, @.endIndex INT)
declare @.maxrts bigint;
select @.maxrts = (select Distinct max((((wins)*((wins*100)/((games+1)*4))))) from unit);
if @.maxrts = 0
set @.maxrts = 1;
declare @.maxValue bigint;
set @.maxValue=2500
select *
from (select *, (select unit from UnitTypes where UnitTypes.unitid = unit.UnitType) as unitTypeName, ((@.maxValue*((wins)*((wins*100)/((games+1)*4))))/@.maxrts ) as trs2 from Unit) as Unit1
inner join
(select Unit3.trs, count(*) as UnitIndex --< here is UnitIndex
from
(select ((@.maxValue*((wins)*((wins*100)/((games+1)*4))))/@.maxrts ) as trs from Unit) as unit3
inner join
(select ((@.maxValue*((wins)*((wins*100)/((games+1)*4))))/@.maxrts ) as trs3 from Unit) as Unit2 on Unit3.trs <= Unit2.trs3
group by Unit3.trs) as UnitIndexes
on Unit1.trs2 = UnitIndexes.trs
where UnitIndexes.UnitIndex between @.StartIndex and @.EndIndex
order by Unit1.trs2 DESC

ThanksIt seems like you are making this more complicated than it should be. What are the table definitions and what are you trying to do? Is tsr a calculated field?

To order the records and determine a unit index for them, I would create a subquery in the select clause that counts all records that have a greater tsr.|||Hey,

yea, if I can simplify this, that's be awesome.

I am only looking at one table, I want to select all the fields from it, but relevant fields are:

UNIT
unitid int pk
Name vc(50)
games int
wins int
unittype int (fk referencing UnitTypes, unitid)

UNITTYPES
unitid int PK
unit varchar(20)

tsr is a column I am working out on the fly, it isn't stored in the database. perhaps a better method would be to create a view first with tsr in it, then run the reorder etc after, however I am unsure how views work, and as of yet, I havn't had time to look into them to determine if they are what I should use.

Any help simplifying this would be awesome, lol.

I have solved the issue I had with the script below, however if you can should me however, it is just a repair on the code as it is there, lol

I'll post the repair in here if you'd like to work from that. Basically though, once I determine the column tsr, I then want to determine the 'ranks' of each unit. So basically, units with the same tsr score, get the worse rank, ie, the third and fourth highest scores both get rank 4 (rank = unitindex form the script below). once that is done, I then was to assign a position variable to each row.

I am doing the same thing with a similar query, which a few people are discussing with me in another post. From what they have said, I will be looking at creating a table, with the columns i desire, plus am incrementing 'position' column.

I will then use something like

insert into @.tempTable (the select statement which returns a reordered table with the tsr value).

Thanks
-Ashleigh

-Ashleigh

Friday, February 24, 2012

indexes and query optimization

I have web page that users use to specify sort and selection critieria for a query. So there is a "most common" type set of where clauses, but they can change. Every time the query is run, there are where clauses on multiple columns, and joins from some columns to other tables, and then it is sorted by a different column.

I am wondering if I should have a clustered index on this table since there are so many columns used in where clauses. Would it be useful to cluster on the column it is sorted by, or should I just stick with nonclustered indexes?
ThanksIf you have sort order by different columns, clustered index will not help.
You can have only one clustered index over a table.
But you can still have none clustered indexes over columns you do order by over.