Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The query hints or the Isolation Level are only going to help you in case of any blocking occurs. If you dont mind dirty reads and there are locks during the execution it could be a good idea.</p> <p>The key question is how many data fits the Where clausule you need to use (WHERE BitColumn = 1 AND DateColumn IS NULL) If the subset filtered by that is small compared with the total number of rows, then use an index on both columns, BitColum and DateColumn, including the columns in the select clausule to avoid "Page Lookup" operations in your query plan.</p> <pre><code>CREATE NONCLUSTERED INDEX IX_[Choose an IndexName] ON TableName(BitColumn, DateColumn) INCLUDE (col1, col2, col3) </code></pre> <p>Of course the space needed for that covered-filtered index depends on the datatype of the fields involved and the number of rows that satisfy WHERE BitColumn = 1 AND DateColumn IS NULL.</p> <p>After that I recomend to use a View instead of a CTE:</p> <pre><code>CREATE VIEW [Choose a ViewName] AS ( Select col1, col2, col3 FROM Table1 WHERE Some_Condition UNION ALL Select col1, col2, col3 FROM Table2 WHERE Some_Condition . . . ) </code></pre> <p>By doing that, your query plan should look like 35 small index scans, but if most of the data satisfies the where clausule of your index, the performance is going to be similar to scan the 35 source tables and the solution won't worth it.</p> <p>But You say "Every Table has 5-10 Bit columns and a Corresponding Date column.." then I think is not going to be a good idea to make an index per bit colum. If you need to filter by using diferent BitColums and Different DateColums, use a compute column in your table:</p> <pre><code>ALTER TABLE Table1 ADD ComputedFilterFlag AS CAST( CASE WHEN BitColum1 = 1 AND DateColumn1 IS NULL THEN 1 ELSE 0 END + CASE WHEN BitColum2 = 1 AND DateColumn2 IS NULL THEN 2 ELSE 0 END + CASE WHEN BitColum3 = 1 AND DateColumn3 IS NULL THEN 4 ELSE 0 END AS tinyint) </code></pre> <p>I recomend you use the value 2^(X-1) for conditionX(BitColumnX=1 and DateColumnX IS NOT NULL). It is going to allow you to filter by using any combination of that criteria. By using value 3 you can locate all rows that accomplish: Bit1, Date1 and Bit2, Date2 condition. Any condition combination has its corresponding ComputedFilterFlag value because the ComputedFilterFlag acts as a bitmap of conditions. If you heve less than 8 diferents filters you should use tinyint to save space in the index and decrease the IO operations needed.</p> <p>Then use an Index over ComputedFilterFlag colum:</p> <pre><code>CREATE NONCLUSTERED INDEX IX_[Choose an IndexName] ON TableName(ComputedFilterFlag) INCLUDE (col1, col2, col3) </code></pre> <p>And create the view:</p> <pre><code>CREATE VIEW [Choose a ViewName] AS ( Select col1, col2, col3 FROM Table1 WHERE ComputedFilterFlag IN [Choose the Target Filter Value set]--(1, 3, 5, 7) UNION ALL Select col1, col2, col3 FROM Table2 WHERE ComputedFilterFlag IN [Choose the Target Filter Value set]--(1, 3, 5, 7) . . . ) </code></pre> <p>By doing that, your index coveres all the conditions and your query plan should look like 35 small index seeks.</p> <p>But this is a tricky solution, may be a refactoring in your table schema could produce simpler and faster results.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload