32,000 records - simple SELECT is slow

  • I have "equity_ids" table (32 columns, all varchar(16)) with no Primary Key (SQL 2005)

    Just 3 Non-clustered indexes on varchar fields.

    [SELECT * FROM equity_ids] took 1 min.

    I checked locks - nothing.

    I added Primary Key IDENTITY field, refreshed table STATISTICS.

    Now [SELECT * FROM equity_ids] now takes 23 seconds.

    Still very slow .... ?

    Actually all tables in this Database are weird - no Primary Keys.

    Some kind of dumps.

    All simple SELECT queries are slow in this database, from 1 to 2 min for 30,000 rows.

    It's not server performance issue.

    There are similar tables with 30,000 records in other databases on this server

    and [SELECT * ] takes 5 seconds there.

    Any ideas what's wrong with this table?

  • while select * , just try to trace the query and see how IO performs.

    try moving (copy) the table to another DB which is on another drive and check performance as well.

    is there any compatibility deffernce on the databases?

    is that all other table in the DB performing at this speed?

    Regards
    Durai Nagarajan

  • Doing a SELECT * with no WHERE-clause will always do a scan, either a Table Scan in the case of a heap, or a Clustered Index Scan in the case of a table with a clustered index.

    The difference in time in your first experiment was likely due to fragmentation and the presence of lots of forwarding pointers. Before you added the PK you had a heap, i.e. a table with no clustered index. When you added a PK a clustered index was also added implicitly to support the constraint and so the heap became a clustered table and was defragmented in the process, which improved performance of the scan of all the data.

    In general it is considered good practice to start with the idea that all tables in SQL Server that might be accepting inserts, updates and deletes on a regular basis should have a clustered index. A primary key is also considered a good thing to have. The PK and the clustered index do not have to be on the same set of columns but it you create a PK on a table with no clustered index and you do not specify that the PK should be nonclustered SQL Server will use the PK key columns and generate the supporting index as clustered using those some key columns.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Thanks for response guys !

    Some more clarification.

    Looks like it's a problem with this particular table.

    I did [SELECT TOP 30,000 * FROM ism_secmaster_price] which is located in the same database, it has 107,000 rows

    and it takes 5 seconds to get the results. Although that table has only 9 columns, three of them are float.

    [ism_secmaster_price] table has no PK, but has a clustered index.

    [SELECT TOP 30,000 * FROM equity_ids]

    takes 1 min 5 seconds.

    So the tables are inside the same database and act very differently.

    I suspect something is wrong with [equity_ids].

    How do I check table defragmentation?

  • RVO (3/12/2013)


    Thanks for response guys !

    Some more clarification.

    Looks like it's a problem with this particular table.

    I did [SELECT TOP 30,000 * FROM ism_secmaster_price] which is located in the same database, it has 107,000 rows

    and it takes 5 seconds to get the results. Although that table has only 9 columns, three of them are float.

    [ism_secmaster_price] table has no PK, but has a clustered index.

    [SELECT TOP 30,000 * FROM equity_ids]

    takes 1 min 5 seconds.

    So the tables are inside the same database and act very differently.

    I suspect something is wrong with [equity_ids].

    How do I check table defragmentation?

    Here is a starter query

    SELECT *

    FROM sys.dm_db_index_physical_stats(NULL,NULL,NULL,NULL,'limited')

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply