Showing posts with label index. Show all posts
Showing posts with label index. Show all posts

Monday, March 12, 2012

Quick SQL Server 7.0 index question

At the moment I only know enough about SQL Server to create the databases I
need as a backend for the various internal desktop/web applications I write.
I haven't ever had the time to get seriously involved with internal workings
of SQL Server and we are far too small to employ a DBA.

With any new application performance isn't a problem as the number of
records is very low. Over the life of the application the number of records
obviously increases and performance can start to suffer. My normal cure is
to use the Query Analyzer to suggest and implement indexes that normally get
things back up to speed.

I was wondering whether SQL Server automatically keeps the created indexes
up to date or do I need to be doing anything else either manually or as a
scheduled job? The databases all have auto create statistics and auto update
statistics enabled.

Any pointers gratefully received

Thanks

AlanEnsure the indexes are periodically defragged. Either by a maintenance
plan or DBCC DBReindex. See BOL for details.|||xyz (someone@.somewhere.com) writes:
> I was wondering whether SQL Server automatically keeps the created
> indexes up to date or do I need to be doing anything else either
> manually or as a scheduled job? The databases all have auto create
> statistics and auto update statistics enabled.

What can happen with indexes - as well with the data pages them selves -
is that they can get fragmented. Essentially this means that that the
data is not stored as compactly as it could be. Recall that SQL Server
stores data on pages of 8192 bytes size. And these pages are gathered
eight by eight in extents. One extent is usually only for one table
(but the first page for a table goes into a mixed extent.) If there are
lots of insertions and deletions, you can end up with extents with
only some single rows in them. This leads to that SQL Server will have
to perform more disk reads to get the same amount of data.

The easiest way to approach this is to set up a maintenance job that
performs a DBCC DBREINDEX on all tables. (Although this is not the
most effecient. Tables with little fragmentation are best left alone.)

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns96005592901DYazorman@.127.0.0.1...
> xyz (someone@.somewhere.com) writes:
> > I was wondering whether SQL Server automatically keeps the created
> > indexes up to date or do I need to be doing anything else either
> > manually or as a scheduled job? The databases all have auto create
> > statistics and auto update statistics enabled.
> What can happen with indexes - as well with the data pages them selves -
> is that they can get fragmented. Essentially this means that that the
> data is not stored as compactly as it could be. Recall that SQL Server
> stores data on pages of 8192 bytes size. And these pages are gathered
> eight by eight in extents. One extent is usually only for one table
> (but the first page for a table goes into a mixed extent.) If there are
> lots of insertions and deletions, you can end up with extents with
> only some single rows in them. This leads to that SQL Server will have
> to perform more disk reads to get the same amount of data.
> The easiest way to approach this is to set up a maintenance job that
> performs a DBCC DBREINDEX on all tables. (Although this is not the
> most effecient. Tables with little fragmentation are best left alone.)
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techin.../2000/books.asp

Thanks to both you and louis for the imformation.

Quick simple question!

Hello all,
Using SQL Server 2000, if I create a primary key on a table, does that
automatically create an index - or should one be created manually?
Thanks all,
Jon
This will create the index automatically.
Adi
Jon wrote:
> Hello all,
> Using SQL Server 2000, if I create a primary key on a table, does that
> automatically create an index - or should one be created manually?
> Thanks all,
> Jon
|||Thanks,
Would this be the same for Foreign Keys also?
Thanks,
Jon
"Adi" wrote:

> This will create the index automatically.
> Adi
> Jon wrote:
>
|||No - I have a script here to add indexes to all FKs:
http://www.replicationanswers.com/Script23.asp
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com .
|||Paul, that's great, thanks very much - I notice that it was created in 2005,
will it work for 2000?
Once an index has been created on a table, do you need to re-analyse the
tables or does SQL Server do this automatically? We're using 2000.
Thanks,
Jon
"Paul Ibison" wrote:

> No - I have a script here to add indexes to all FKs:
> http://www.replicationanswers.com/Script23.asp
> Cheers,
> Paul Ibison SQL Server MVP, www.replicationanswers.com .
>
>
|||Jon - sorry - didn't notice that you were on SQL Server 2000. I used to have
a corresponding script for 2000 but can't find it now and guess I never
posted it to my site . I'll do some digging in my backups and see what I
can find.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com .

Friday, March 9, 2012

Quick simple question!

Hello all,
Using SQL Server 2000, if I create a primary key on a table, does that
automatically create an index - or should one be created manually?
Thanks all,
JonThis will create the index automatically.
Adi
Jon wrote:
> Hello all,
> Using SQL Server 2000, if I create a primary key on a table, does that
> automatically create an index - or should one be created manually?
> Thanks all,
> Jon|||Thanks,
Would this be the same for Foreign Keys also?
Thanks,
Jon
"Adi" wrote:

> This will create the index automatically.
> Adi
> Jon wrote:
>|||No - I have a script here to add indexes to all FKs:
http://www.replicationanswers.com/Script23.asp
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com .|||Paul, that's great, thanks very much - I notice that it was created in 2005,
will it work for 2000?
Once an index has been created on a table, do you need to re-analyse the
tables or does SQL Server do this automatically? We're using 2000.
Thanks,
Jon
"Paul Ibison" wrote:

> No - I have a script here to add indexes to all FKs:
> http://www.replicationanswers.com/Script23.asp
> Cheers,
> Paul Ibison SQL Server MVP, www.replicationanswers.com .
>
>|||Jon - sorry - didn't notice that you were on SQL Server 2000. I used to have
a corresponding script for 2000 but can't find it now and guess I never
posted it to my site . I'll do some digging in my backups and see what I
can find.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com .

Wednesday, March 7, 2012

Quick method to determine relative position of the index entry

I have a large table with some indexes.
And I have a value for an indexed field. So I'd like to get the order of the first entry of this value in a specified index.

I'd like to write a stored procedure, which returns such number for given value and index name.

How can I do it?I have found a quik decision!

select count(distinct indexed_col_name)
from Table
where index_col_name < value

Other ideas (with Cursor , for example) are slower.