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. Help in understanding a simple query result

Help in understanding a simple query result

Scheduled Pinned Locked Moved Database
databasemysqlhelp
8 Posts 3 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.
  • S Offline
    S Offline
    Snowman58
    wrote on last edited by
    #1

    I am an SQL newbee & not understanding the results I am getting from a seemingly simple query. The query itself is returning the correct result but taking far too long. So I ran Explain to see what was happening. Sure enough more data is being examined than expected. But I can't figure out why. I have tried reordering the joins, but get the same result. Perhaps someone could educate me why it is returning so much data. Note: All ID's are integer types. The ID of the respective tables are the primary key and the ID's used in the Assemdata table are indexed. MySQL ver 5.x select assemdata.AssemNum innerpack.Descrip hanger.Descrip from hanger join innerpack join assemdata where assemdata.Assemnum >=60000 and assemdata.Assemnum <=60050 and assemdata.InnerPackTypeID = innerpack.InnerPackID and assemdata.hangerID = hanger.HangerID Explain Result: ID select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE innerpack ALL PRIMARY (Null) (Null) (Null) 3 Using where 1 SIMPLE assemdata ALL (Null) (Null) (Null) (Null) 2798 Using where 1 SIMPLE hanger ALL PRIMARY (Null) (Null) (Null) 2 Using where Query Result: AssemNum Descrip Descrip 60001 None Sawtooth 60002 None Sawtooth 60003 None Sawtooth ...... 49 Rows of data. Thanks for any suggestions!

    A 1 Reply Last reply
    0
    • S Snowman58

      I am an SQL newbee & not understanding the results I am getting from a seemingly simple query. The query itself is returning the correct result but taking far too long. So I ran Explain to see what was happening. Sure enough more data is being examined than expected. But I can't figure out why. I have tried reordering the joins, but get the same result. Perhaps someone could educate me why it is returning so much data. Note: All ID's are integer types. The ID of the respective tables are the primary key and the ID's used in the Assemdata table are indexed. MySQL ver 5.x select assemdata.AssemNum innerpack.Descrip hanger.Descrip from hanger join innerpack join assemdata where assemdata.Assemnum >=60000 and assemdata.Assemnum <=60050 and assemdata.InnerPackTypeID = innerpack.InnerPackID and assemdata.hangerID = hanger.HangerID Explain Result: ID select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE innerpack ALL PRIMARY (Null) (Null) (Null) 3 Using where 1 SIMPLE assemdata ALL (Null) (Null) (Null) (Null) 2798 Using where 1 SIMPLE hanger ALL PRIMARY (Null) (Null) (Null) 2 Using where Query Result: AssemNum Descrip Descrip 60001 None Sawtooth 60002 None Sawtooth 60003 None Sawtooth ...... 49 Rows of data. Thanks for any suggestions!

      A Offline
      A Offline
      A Wong
      wrote on last edited by
      #2

      Shouldn't you be inner joining on condition?

      S 1 Reply Last reply
      0
      • A A Wong

        Shouldn't you be inner joining on condition?

        S Offline
        S Offline
        Snowman58
        wrote on last edited by
        #3

        I have tried specifying the joins as "inner" - it made no difference. (My understanding is that MySQL interperts a join as "inner" unless otherwise specified.) I have also tried being specific on the join member i.e. join on hangerID. Also made no difference. Thanks!

        A 1 Reply Last reply
        0
        • S Snowman58

          I have tried specifying the joins as "inner" - it made no difference. (My understanding is that MySQL interperts a join as "inner" unless otherwise specified.) I have also tried being specific on the join member i.e. join on hangerID. Also made no difference. Thanks!

          A Offline
          A Offline
          A Wong
          wrote on last edited by
          #4

          Have you tried selecting from the smallest table first?

          S 1 Reply Last reply
          0
          • A A Wong

            Have you tried selecting from the smallest table first?

            S Offline
            S Offline
            Snowman58
            wrote on last edited by
            #5

            Yes - I have tried all combinations I could think of. The Explain output is identical no matter what order I use. Frustrating! Thanks

            S 1 Reply Last reply
            0
            • S Snowman58

              Yes - I have tried all combinations I could think of. The Explain output is identical no matter what order I use. Frustrating! Thanks

              S Offline
              S Offline
              suppaman
              wrote on last edited by
              #6

              Hello Mr. Member 4723455, I always think it's better to put relational condition in "ON" clause of JOIN. You can try: select assemdata.AssemNum,innerpack.Descrip,hanger.Descrip from hanger join assemdata on assemdata.hangerID = hanger.HangerID join innerpack on assemdata.InnerPackTypeID = innerpack.InnerPackID where assemdata.Assemnum >=60000 and assemdata.Assemnum <=60050 Bye, (sorry for my bad English) :^)

              S 1 Reply Last reply
              0
              • S suppaman

                Hello Mr. Member 4723455, I always think it's better to put relational condition in "ON" clause of JOIN. You can try: select assemdata.AssemNum,innerpack.Descrip,hanger.Descrip from hanger join assemdata on assemdata.hangerID = hanger.HangerID join innerpack on assemdata.InnerPackTypeID = innerpack.InnerPackID where assemdata.Assemnum >=60000 and assemdata.Assemnum <=60050 Bye, (sorry for my bad English) :^)

                S Offline
                S Offline
                Snowman58
                wrote on last edited by
                #7

                Suppaman, I have tried it both ways i.e. using "join on table1.id=table2.id" & using "where table1.id=table2.id". The results from an Explain Select are identical. But I agree the on method is easier to read. Thanks!

                S 1 Reply Last reply
                0
                • S Snowman58

                  Suppaman, I have tried it both ways i.e. using "join on table1.id=table2.id" & using "where table1.id=table2.id". The results from an Explain Select are identical. But I agree the on method is easier to read. Thanks!

                  S Offline
                  S Offline
                  suppaman
                  wrote on last edited by
                  #8

                  You can try using views: first create a view to filter out elements of assemdata with Assemnum >=60000 and Assemnum <=60050 (50 elems) then join this view with the other two table. In this way the query plan should be different and the query execution faster (I think). Bye ^__^

                  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