An Index Reduces Performance of SELECT Queries

Поділитися
Вставка
  • Опубліковано 2 лют 2025

КОМЕНТАРІ • 83

  • @PinalDaveSQLAuthority
    @PinalDaveSQLAuthority  5 років тому +13

    Hi All, Those who want free scripts to identify unused indexes, can register here: go.sqlauthority.com and it will send you script immediately.

  • @chuck7494
    @chuck7494 5 років тому +1

    That is really surprising. I would never had noticed that an index could cause such performance degradation!!! THANK YOU!!! I ALWAYS look to you for answers to the really tough ones. Glad you are here!!!!!!! KEEP UP THE GREAT WORK!

  • @smwnl9072
    @smwnl9072 5 років тому +8

    Power. No one explains Tsql as clear and interesting as you ❤

  • @sanujss
    @sanujss 5 років тому +2

    Have attended one of your sessions in Bangalore Microsoft TechEd few years back. Following you from then on blog. No one else explains these things better. Thanks a ton

  • @RalphDon21
    @RalphDon21 Рік тому +1

    There's my teacher, right there. Thanks, sir!

  • @slavamurygin4849
    @slavamurygin4849 5 років тому +1

    Pinal, saw that presentation on PASS Summit. Which version of SQL Server you are using?
    2016 does not show that behavior:
    Microsoft SQL Server 2016 (SP2-GDR) (KB4293802) - 13.0.5081.1 (X64) Jul 20 2018 22:12:40
    Copyright (c) Microsoft Corporation Developer Edition (64-bit)
    on Windows 8.1 Enterprise 6.3 (Build 9600: ) (Hypervisor)

    • @PinalDaveSQLAuthority
      @PinalDaveSQLAuthority  5 років тому

      I used SQL Server 2017 for this demo and SQL Server 2019 for PASS.

    • @slavamurygin4849
      @slavamurygin4849 5 років тому

      @@PinalDaveSQLAuthority Thanks. I can see it now

  • @mehdighapanvari9950
    @mehdighapanvari9950 Рік тому +1

    Hi Pinal! Please use temporary table instead of subquery in your scenario. Thanks!

  • @richard75013U
    @richard75013U 5 років тому +13

    ​ Pinal Dave You are exploiting a rare scenario and attempting to invoke fear for those that don’t know that this can occur so they will hire you as identified by the quote from you below "we can discuss during the consultation or during training". Another person is asking you how to identify missing indexes and you tell him to subscribe to some site and you will hand out 3 free scripts when you could have just replied back informing he/she to use the built-in DMV sys.dm_db_index_usage_stats. I have seen some of your videos and liked them, but this is a step a little too far in my opinion. 99.99999% of the time your performance problem will NOT be from an index but from an overwhelmed subsystem resource, mis/non-configured server settings, mis/non-configured database settings, missing indexes, your query and yes the poor architecture a db may have which can help to induce performance issues.

    • @PinalDaveSQLAuthority
      @PinalDaveSQLAuthority  5 років тому +9

      Hi Richard,
      Thanks for your comment. I appreciate your comment and I totally agree there are lots of things beyond the index. During SQL Server consultancy, along with the index, you need to look at lots of different things - just to name few -
      Server/Instance Level Configuration Check
      I/O distribution Analysis
      SQL Server Resource Wait Stats Analysis
      TempDB Review
      Database Files (MDF, NDF) and Log File Inspection
      Log Reviews (Windows Event Logs, SQL Server Error Logs and Agent Logs)
      DBCC Best Practices Implementations
      When you subscribe to get free scripts you get the complete script which also generates the drop script of the unused index. However, for advanced users, you can use sys.dm_db_index_usage_stats as well.
      Here is the link for my consultancy, where we discuss indexes and lot more content: blog.sqlauthority.com/comprehensive-database-performance-health-check/
      All the advanced users do not need any consultants, as they can just do all of them myself. However, those who are busy can consider hiring others to do the task for them.
      Again, truly appreciate you taking time to leave a comment. Thank you!

  • @paruchurusurendra
    @paruchurusurendra 3 роки тому +1

    did we can retrieve the JSON data as early as possible from the table we have 30 columns

  • @ericrouach
    @ericrouach 4 роки тому +1

    You are a Saint! Thanks a lot!

  • @baidya4272
    @baidya4272 3 роки тому +1

    Can you pls suggest on below:
    Table A with cols ID, Name, Message
    There are two non clustered indexes
    1. Index1 with index_keys as Name,Message
    2. Index2 with index_keys as Message,Name
    These are duplicate indexes but column order is different.
    Is this ok or probable reason of performance issues?

  • @Ganeshay-09
    @Ganeshay-09 2 роки тому +1

    are u provide training of sql basic to advance

  • @ArindamGangulyKolkata
    @ArindamGangulyKolkata 4 роки тому +1

    Hi Pinal here the performance gets degraded when you create two indexes in the order of A, B and B, A. Are they not covering index ? Should B, A be used ? What happens if I add another index of the order C, D ? Just a query,

    • @PinalDaveSQLAuthority
      @PinalDaveSQLAuthority  4 роки тому

      Your question is very valid and it should be investigated. I will have to try myself.

  • @IrawanSoetomo
    @IrawanSoetomo 5 років тому +1

    have you tried to use OPTION (RECOMPILE) at the bottom? how would it affect this behavior?

    • @PinalDaveSQLAuthority
      @PinalDaveSQLAuthority  5 років тому

      In longer demo we do experiment with that... Absolutely no difference in results you are seeing.

  • @nareshkoudagani6969
    @nareshkoudagani6969 5 років тому +6

    The subject of the Video should be, a Un used INDEX Reduces Performance of SELECT Queries

  • @Harshk2501
    @Harshk2501 4 роки тому +2

    Very nice explanation sir. I request you to please provide a explanation video for understanding deadlock graph.

  • @allentertainmentin1565
    @allentertainmentin1565 4 роки тому +1

    Hey Pinal, if scan 20% and lookeup 10% update 70%.Is it unused index? Yes/No ? then how

  • @marufkhan1395
    @marufkhan1395 4 роки тому +1

    I would have love to see the video including a cluster index and then creating a non cluster index would have shown some different results.

  • @Chinnurockbells
    @Chinnurockbells 3 роки тому +1

    How to reset indexes sir,

    • @PinalDaveSQLAuthority
      @PinalDaveSQLAuthority  3 роки тому

      Sorry, I do not understand.

    • @Chinnurockbells
      @Chinnurockbells 3 роки тому

      @@PinalDaveSQLAuthority I create indexes long back then how can I reset the indexes. Is it requires? If requires how to reset existing indexes

  • @meghanatoraskar5301
    @meghanatoraskar5301 3 роки тому +1

    Cool thanks

  • @ThierryC2373
    @ThierryC2373 5 років тому +2

    I think it is more how you define your index(es) than anything else that impacts the performances.

  • @gafajardog
    @gafajardog 5 років тому +2

    Just as a new index affects the performance of existing queries, so an existing index can affect future queries

  • @mjzvsuiza
    @mjzvsuiza 5 років тому +5

    If you show the execution plan as XML you will probably see that the index is ACTUALLY being used. The statistics of the new index are being used to determine the execution plan. I agree in a detrimental way. but the index is being used.

    • @PinalDaveSQLAuthority
      @PinalDaveSQLAuthority  5 років тому +4

      Great point. However, what I wanted to stress in this video was also the point that when we create an index, it can also reduce the performance of SELECT statement which people usually do not think. Many people just leave their indexes as it is and have no idea how they impact other queries and particularly SELECT queries.
      This is just one of the tip, there are so many misconceptions out there and lots of people need to learn about them, otherwise, they will spend countless hours in doing performance debugging.

  • @curtisbrowne2710
    @curtisbrowne2710 4 роки тому

    I was not able to reproduce this behavior on SQL2019. I was using AdventureWorks2017. As long as the first index is there is behaves nicely even if the second one exists. I tried compatibility levels 130, 140 and 150. Are we sure what you present is true? Does it depend on the database?

    • @PinalDaveSQLAuthority
      @PinalDaveSQLAuthority  4 роки тому

      I tried it and it works in SQL Server 2019 as well just like the other one.

    • @curtisbrowne2710
      @curtisbrowne2710 4 роки тому

      @@PinalDaveSQLAuthority Yes, it works on AdventureWorks2014, but I it doesn't seem to work on the AdventureWorks2017 version of the database.What would cause the behavior to change?

    • @PinalDaveSQLAuthority
      @PinalDaveSQLAuthority  4 роки тому

      @@curtisbrowne2710 I tried with 2017 with different compatibility and it works too. I am really not sure.

    • @martinsmith8670
      @martinsmith8670 4 роки тому

      @@curtisbrowne2710 did you take into account that the behaviour depends on index id? So it matters what order the indexes are created in? dba.stackexchange.com/questions/259780/creating-an-index-that-is-not-used-by-a-select-query-reduces-performance-of-th/259853#259853

  • @ChintanCG
    @ChintanCG 5 років тому +2

    how do i find unused indices in the database?

    • @PinalDaveSQLAuthority
      @PinalDaveSQLAuthority  5 років тому +1

      Chintan, You can go to go.sqlauthority.com and subscribe there and in the response, you will get three free performance tuning scripts.

    • @natalirozin6965
      @natalirozin6965 5 років тому +1

      sys.dm_db_index_usage_stats

  • @fitnessgroupofindia4423
    @fitnessgroupofindia4423 5 років тому +1

    Sir your voice is very good...

  • @mohsenbolhasani7880
    @mohsenbolhasani7880 5 років тому +1

    Hi
    Why did this happen?
    Is there any explanation?

    • @PinalDaveSQLAuthority
      @PinalDaveSQLAuthority  5 років тому

      Great question, I really wish the explanation was that simple. It is pretty long and I usually cover that in my training. However, for the simple version, you can watch some of my free videos here and I explain it there. blog.sqlauthority.com/free-learning-videos/

    • @martinsmith8670
      @martinsmith8670 5 років тому +1

      explanation here dba.stackexchange.com/questions/259780/creating-an-index-that-is-not-used-by-a-select-query-reduces-performance-of-th/259853#259853

  • @thearchibaldtuttle
    @thearchibaldtuttle 4 роки тому +1

    Wow! Didn't knew this happens but I'm only a manager :-)

  • @caparn100
    @caparn100 5 років тому +7

    I spent over 10 minutes watching this video but you didn't say why the select was slower with an unused index. So this video is not really very helpful.
    Are you hoping we will pay you to tell us that or are you expecting us to google to find out why?

    • @PinalDaveSQLAuthority
      @PinalDaveSQLAuthority  5 років тому +2

      Great question my friend. The topic was to show that Index reduces the performance of SELECT queries and not to go into the details about why as it would a very long deeper conversation which we can discuss during the consultation or during training.
      I hope from this video I was clear that just like Insert, Update and Delete, an Index can slow down a SELECT statement as well. Let me know if that was not clear.
      ... and thank you for watching this one!

    • @martinsmith8670
      @martinsmith8670 5 років тому +1

      Explanation here dba.stackexchange.com/questions/259780/creating-an-index-that-is-not-used-by-a-select-query-reduces-performance-of-th/259853#259853

  • @alajpurinikhila9225
    @alajpurinikhila9225 2 роки тому

    find top 1-3 queries that are executed the most

  • @lookingforronfalter
    @lookingforronfalter 5 років тому +3

    you've just happened to come across a bug in the qry optimizer. It picked a poor plan! I would say this is more rare than you think. Not impossible of course, but probably not to lose sleep over. If you have adequate IO/CPU headroom, and your server is humming along performing well, don't waste your time looking for these obscure problems

    • @PinalDaveSQLAuthority
      @PinalDaveSQLAuthority  5 років тому +1

      Hi there, honestly no. This is what I have been seeing at many of my clients from the last 10 years. During consultation, we have found many cases which are similar to this one and also this is not a bug but rather expected behavior.

    • @lookingforronfalter
      @lookingforronfalter 5 років тому +2

      @@PinalDaveSQLAuthority if you're looking at high resource consuming queries, you should detect this and fix it any way.. but I disagree, it's a bug when it comes to query optimization. It's a failure on the optimizer.. I'm sure David DeWitt would agree

    • @PinalDaveSQLAuthority
      @PinalDaveSQLAuthority  5 років тому +1

      @@lookingforronfalter let us agree to disagree. However, as I mentioned Indwx Tuning is not I start my day with... There are many more stuff you need to do it before it, and I am sure you agree with that part.

  • @FrancescoMantovani
    @FrancescoMantovani 4 роки тому +1

    I think I have to pay that 1:1 session for my managers. If they don't listen to me at least they will listen to you :|

  • @rajib2k5
    @rajib2k5 5 років тому +1

    lol... that's a lot of forward statement my friend. :D

  • @techsapphire
    @techsapphire 5 років тому +1

    This is very wrong way to present this problem. If I have to tune this query that first thing will he getting rid of lazy spool. By using cte or temp table . Creating index is not even a solution in this. But I really fan of your blog. Thank you for posting this.

    • @PinalDaveSQLAuthority
      @PinalDaveSQLAuthority  5 років тому +2

      Thanks for watching the video. Please note that the goal of the video was not to tune the query but to demonstrate that index can reduce the performance of the index. I hope you got that message, please.
      Once you agree to the point we can focus on alternatives to fix the queries. Additionally, the first index solves the problem as well. It is just an addition index, which is not used is creating a problem.
      Besides CTE or temp table and indexes there are few more tricks also there where we can get query Performance without changing the code too... In some future video, I will cover it.
      Thank you again for watching the video, I think we both agreed that indexes are not good so we are the same page.

    • @techsapphire
      @techsapphire 5 років тому +1

      @@PinalDaveSQLAuthority Now when I see it, I agree with you. It is specially for DBA who just think index is solution to every thing. Thank you keep posting :)

  • @srinivasannaswamy7658
    @srinivasannaswamy7658 4 місяці тому

    why on earth anyone wants average on productid . Query is reading same table twice with a sub query in where clause . can be easily written with over clause and partition by . Best Practices of writing query not followed