SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    237
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with proper index, or proper query?

    The indexes for C and V only look at 1 row, but the index for M looks at ALL rows (but the PRIMARY is for ID_MEM). It says that PRIMARY is a possible key, but it isn't using it.

    The main content that I am returning is from C, yet it looks like the query might be reading this in an inefficient way.

    Code:
    SELECT V.ti, V.vti, V.ad, C.* , M.av, M.me, M.ID_MEM
    FROM c_n AS C
    JOIN vis AS V ON V.ID = C.viid
    JOIN mes AS M ON M.ID_MEM = C.uid
    WHERE (
    (
    C.aid =74410
    AND C.sil =1
    )
    OR (
    C.oai LIKE '%;;word;;%'
    AND C.silm NOT LIKE '%;;word2;;%'
    )
    )
    AND C.uid !=74410
    AND C.uid >0
    ORDER BY C.ID DESC
    LIMIT 0 , 25
    Help?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    could you please do a SHOW CREATE TABLE for each table, and an EXPLAIN on the query
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    237
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply.

    For security reasons, I have sent you a PM with that data.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    had a look, but sorry, it's too complicated for me
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    just a quick afterthought...

    perhaps break the query up into two parts, eliminating the OR, and UNIONing them together --
    Code:
    SELECT...
      FROM ...
     WHERE C.aid =74410
       AND C.sil =1
       AND C.uid !=74410
       AND C.uid >0
    UNION ALL
    SELECT ...
      FROM ...
     WHERE C.oai LIKE '%;;word;;%'
       AND C.silm NOT LIKE '%;;word2;;%'
       AND C.uid !=74410
       AND C.uid >0
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    237
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I played around with UNION, but I don't think that is exactly what I want.

    For now, I switched this over to 1 query with 2 tables, and then a query in the while() loop to grab the data from the third table. This results in 26 queries on the page, but it is much faster than just 1 bad query.

    I think this can probably be optimized more. I'll have to dig deeper.

    Thanks for the help!

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by jetnine View Post
    I played around with UNION, but I don't think that is exactly what I want.
    why not?

    what i posted is exactly equivalent to what you posted
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •