SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Free me php klassicd's Avatar
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sluggish query need help

    Can anyone help me with optimizing this query? It seems every time it is run (or a query similar to it) it slows down MYSQL greatly and sometimes even hangs our system.

    Code:
    SELECT l.* FROM leads l, leads_groups lg, leads_employees le 
    WHERE 1 AND l.type='buyer' AND lg.lead_id=l.id AND le.lead_id=l.id AND le.employee_id='10' 
    ORDER BY l.lastname ASC LIMIT 10

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    do you have any indexes defined on the tables?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    Free me php klassicd's Avatar
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937
    do you have any indexes defined on the tables?
    For the leads table yes I have a primary key. None of the other tables have an index.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    alter table leads_groups add index (lead_id)

    alter table leads_employees add index (lead_id)

    and possibly

    alter table leads_employees add index (employee_id)
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    Free me php klassicd's Avatar
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937
    alter table leads_groups add index (lead_id)

    alter table leads_employees add index (lead_id)

    and possibly

    alter table leads_employees add index (employee_id)
    thanks r937. that speed things up greatly. should I index the other column in each of those tables also (g.group_id, e.lead_id)?

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    if you search on them, yes

    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
  •