Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Would an index make this query faster?

Would an index make this query faster?

Scheduled Pinned Locked Moved Database
databasesalesperformancequestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    khun_panya
    wrote on last edited by
    #1

    select cs.customer_name, st.staff_name
    from Customer cs
    inner join Staff st on cs.city_id=st.city_id

    (just a sample query, dont ask for its purpose) I'm quite sure that we need an index on st.city_id to speed things up. But would an index on cs.city_id make any different? Because it looks like we have to scan entire Customer table anyway.

    M L G 3 Replies Last reply
    0
    • K khun_panya

      select cs.customer_name, st.staff_name
      from Customer cs
      inner join Staff st on cs.city_id=st.city_id

      (just a sample query, dont ask for its purpose) I'm quite sure that we need an index on st.city_id to speed things up. But would an index on cs.city_id make any different? Because it looks like we have to scan entire Customer table anyway.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Assuming you are using SQL Server, turn on the Profiler in SSMS and run the query, it will then recommend an index if required.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • K khun_panya

        select cs.customer_name, st.staff_name
        from Customer cs
        inner join Staff st on cs.city_id=st.city_id

        (just a sample query, dont ask for its purpose) I'm quite sure that we need an index on st.city_id to speed things up. But would an index on cs.city_id make any different? Because it looks like we have to scan entire Customer table anyway.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        khun_panya wrote:

        But would an index on cs.city_id make any different?

        Most probably it will. The query optimizer decides whether to use an index or not depending on various criteria. If the number of rows in the table is very less, it might do a table scan rather than an index seek. But this is purely left to the optimizer to decide.

        khun_panya wrote:

        Because it looks like we have to scan entire Customer table anyway.

        What makes you think so? If execution plan shows a table scan, then either an index does not exist or the number of rows is very less to warrant an index seek.

        K 1 Reply Last reply
        0
        • K khun_panya

          select cs.customer_name, st.staff_name
          from Customer cs
          inner join Staff st on cs.city_id=st.city_id

          (just a sample query, dont ask for its purpose) I'm quite sure that we need an index on st.city_id to speed things up. But would an index on cs.city_id make any different? Because it looks like we have to scan entire Customer table anyway.

          G Offline
          G Offline
          GuyThiebaut
          wrote on last edited by
          #4

          There is a good chance that an index would speed things up. The advice given so far is sound, however.. indexes are a bit of an art and like all arts you have to experiment occasionally. Two areas that affect indexes are the number of rows and the frequency of updates and inserts. For this reason you need to understand some of the theory behind indexes and also be willing to experiment with adding indexes to see what happens and learn from that. I don't have any links but I would suggest giving yourself a good hour to google and read up on indexes as it will stand you in good stead.

          “That which can be asserted without evidence, can be dismissed without evidence.”

          ― Christopher Hitchens

          J 1 Reply Last reply
          0
          • G GuyThiebaut

            There is a good chance that an index would speed things up. The advice given so far is sound, however.. indexes are a bit of an art and like all arts you have to experiment occasionally. Two areas that affect indexes are the number of rows and the frequency of updates and inserts. For this reason you need to understand some of the theory behind indexes and also be willing to experiment with adding indexes to see what happens and learn from that. I don't have any links but I would suggest giving yourself a good hour to google and read up on indexes as it will stand you in good stead.

            “That which can be asserted without evidence, can be dismissed without evidence.”

            ― Christopher Hitchens

            J Offline
            J Offline
            Jorgen Andersson
            wrote on last edited by
            #5

            Here's[^] a really good link about indexes.

            Wrong is evil and must be defeated. - Jeff Ello[^]

            1 Reply Last reply
            0
            • L Lost User

              khun_panya wrote:

              But would an index on cs.city_id make any different?

              Most probably it will. The query optimizer decides whether to use an index or not depending on various criteria. If the number of rows in the table is very less, it might do a table scan rather than an index seek. But this is purely left to the optimizer to decide.

              khun_panya wrote:

              Because it looks like we have to scan entire Customer table anyway.

              What makes you think so? If execution plan shows a table scan, then either an index does not exist or the number of rows is very less to warrant an index seek.

              K Offline
              K Offline
              khun_panya
              wrote on last edited by
              #6

              Shameel wrote:

              khun_panya wrote:

              Because it looks like we have to scan entire Customer table anyway.

              What makes you think so? If execution plan shows a table scan, then either an index does not exist or the number of rows is very less to warrant an index seek.

              I have not checked for real execution plan. But above query do select with "no filter". So it would have to iterate all the rows in table. That's why I doubt that an index helps in this situation.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups