SQL Statement - How can Improve speed with indexing

后端 未结 4 1869
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-15 05:01

I have a script that has to look throught over 2.5 million records to find if a member that has an unread email. I want to know what can be done to improve its speed. Curr

4条回答
  •  清酒与你
    2021-01-15 05:25

    As a thumb rule, every field on which you perform frequent filters (where conditions) must be indexed.

    Again, as a thumb rule, I follow these criteria:

    1. Every key fields (primary or foreign) must be indexed
    2. Every date field on which I have to perform frequent lookups must be indexed
    3. Although I avoid it, if I need to perform frequent searches on char or varchar fields, I index them as well

    Notice that it is easy to fall in the temptation of indexing everything. Don't do it. Be careful and design your indexes with the best cost - benefit relationship.

    I'm a MySQL user, and I don't how to do it in SQL server, but there must be a way to show the execution plan of your query (in MySQL it is explain select...). Try to show the execution plan, and then decide on that basis which are the fields you need to index.

提交回复
热议问题