For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:
The optimization process and recommendations:
- Avoid Calling Functions With Indexed Columns (query line: 23): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `aAuthor` is indexed, the index won’t be used as it’s wrapped with the function `Contains`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
- Avoid Calling Functions With Indexed Columns (query line: 23): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `aBody` is indexed, the index won’t be used as it’s wrapped with the function `Contains`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
- Avoid Calling Functions With Indexed Columns (query line: 23): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `aHeadline` is indexed, the index won’t be used as it’s wrapped with the function `Contains`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
- Avoid Calling Functions With Indexed Columns (query line: 23): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `aSubhead` is indexed, the index won’t be used as it’s wrapped with the function `Contains`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
- Avoid Calling Functions With Indexed Columns (query line: 23): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `aSummary` is indexed, the index won’t be used as it’s wrapped with the function `Contains`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
- Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `articles` ADD INDEX `articles_idx_alive_full_story_adate` (`aLive`,`full_story`,`aDate`);
The optimized query:
SELECT
TOP 50 ROW_NUMBER() OVER (ORDER
BY
articles.aDate DESC) AS Row,
articles.aID,
articles.aHeadline,
articles.aAuthor,
articles.aDate,
articles.TopStory,
articles.SiteSectionID,
articles.PrintSection,
articles.aSummary,
articles.small_image,
articles.aBody
FROM
articles
WHERE
articles.aLive = -1
AND articles.full_story = -1
AND articles.aDate >= '9/3/2012'
AND articles.aDate <= '10/3/2012'
AND (
Contains((articles.aAuthor, articles.aBody, articles.aHeadline, articles.aSubhead, articles.aSummary), '"kodak"')
)
ORDER BY
articles.aDate DESC