Sql indexes vs full table scan

点点圈 提交于 2019-12-24 00:52:50

问题


While writing complex SQL queries, how do we ensure that we are using proper indexes and avoiding full table scans? I do it by making sure I only join on columns that have indexes(primary key, unique key etc). Is this enough?


回答1:


Look at the execution plan of the query to see how the query optimizer thinks things must be retrieved. The plan is generally based on the statistics on the tables, the selectivity of the indices and the order of the joins. Note that the optimizer can decide that performing a full table scan is 'cheaper' than index lookup.

other Things to look for:

  • avoid subqueries if possible.

  • minimize the use of 'OR'-predicates
    in the where clause




回答2:


Ask the database for the execution plan for your query, and proceed from there.

Don't forget to index the columns that appear in your where clause as well.




回答3:


It is hard to say what is the best indexing because there are different strategies depend on situation. Still there are coupe things you should now about indexes.

  1. Index SOMETIMES increase performance on select statement and ALWAYS decrease performance on insert and update.
  2. To index table it is not necessary to make it as key on certain field. Also, real life indexes almost always include several fields.
  3. Don't create any indexes for "future purposes" if your performance is satisfactory. Even if you don't have indexes at all.
  4. Always try to analyze execution plan when tuning indexes. Don't be afraid to experiment.

      +
  5. Table scan is not always bad thing.

That is all from me.




回答4:


Use Database Tuning Advisor(SQL Server) to analyse your query. It will suggest necessary indexes to add to tune your query performance



来源:https://stackoverflow.com/questions/4810804/sql-indexes-vs-full-table-scan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!