how to improve performance on a query

  • This is the case:

    We have an accounting vendor package that is required to run a transaction against a table to search for a particular set of rows. The query is very simple but it takes about 7.30 minutes to run. Apparently, this process needs to run several times a day, therefore it's slowing other users down as soon as it hits the server.

    Here are the particulars:

    The table: it has 10.5 million records with about 50 columns and only expected to grow about 150,000 records per month.

    The query: Select * from tablename where columnname = 'ON' (please don't shoot yet), I'm working on getting their developer team to tell me why they need all the columns and if not which ones are the ones they need.

    The filtering column by has only two values ON or OFF and it's a varchar(15).

    The primary key on the table is a varchar(15) and it seems to be all integers (don't ask me why they didn't use an integer data type from the beginning, vendor package remember, I know.)

    The primary key has a clustered index key

    There are four other non-clustered keys present in the table

    The filtering column does not have an index key at all.

    Also, the server has 2 CPU and 12GB of RAM (9GB are assigned to SQL), running SQL Server 2008 R2

    My question is how can we improve this simple query. I tried adding a separate column using bit/integer to filter by this and still is very slow.

    Any advise would be very appreciated.

    Thank you.

  • If you can, please post the exact query, as well as the index definitions.

    Most importantly, please attached the execution plan...

    To start, add a non clustered index on the filtered column and see if that helps.

    ______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience

  • The actual select statement is as follows:

    Select * from tablename where columnname = 'OFF' (this value is either ON/OFF).

    The only index set on the table is on the primary key defined as a clustered index.

    I added a non-clustered index to the table and didn't seem to make a difference.

  • An index on a dual-valued column isn't likely to be of much use, unless one of the values occurs significantly less often than the other, and that's the value you search for in your query.

    Here are some things that spring to mind that you could try. You'll need to test thoroughly, because even if one of these things improves the performance of the query in question, it might make things worse elsewhere.

    * Make sure you're rebuilding your clustered index regularly. This will keep wasted space to a minimum and therefore reduce the IO needed for a clustered index scan

    * Review the design of your table so that, for example, "On" and "Off" are stored as char(3) instead of varchar(20). This should mean that more rows can fit on each page, which will also reduce IO

    * Consider creating an indexed view that includes the columns you need for your query (once you find out what those columns are)

    * Consider creating a covering index consisting of only the columns you need for your query

    John

  • Honestly from what you described the time sounds about as good as it is going to get. Assuming roughly 50% of your rows are "ON" then you are selecting about 5 million rows with 50 columns. 7 1/2 minutes sounds about like what I would expect. Why all columns and what in the world are you doing with 5 million rows?

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Honestly from what you described the time sounds about as good as it is going to get. Assuming roughly 50% of your rows are "ON" then you are selecting about 5 million rows with 50 columns. 7 1/2 minutes sounds about like what I would expect. Why all columns and what in the world are you doing with 5 million rows?

    Why all columns?

    The SQL statement in question is from a "batch" process that runs against our database during the day, a few times a day, it's a vendor package that runs this statement. We're trying to get the vendor to change this query to only select the columns needed. That on its own it has been a good argument and challenge.

    Why 5 millions rows?

    It's all the transaction history that we have in our accounting system. After the selection, depending on the results return, there's another set of events that are happening afterwards.

  • HildaJ (4/14/2014)


    It's all the transaction history that we have in our accounting system. After the selection, depending on the results return, there's another set of events that are happening afterwards.

    Can you change the process so that you don't keep processing rows that have already been processed? If downstream these already processed rows are going to be ignored it would greatly help performance to eliminate them from this query too.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • I have to ask this question, what is the batch process doing?

Viewing 8 posts - 1 through 7 (of 7 total)

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