Results 1 to 3 of 3

Thread: Optimization of query

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Optimization of query

    Hello,
    I have written a query but I am not sure if it is efficient.
    There are two tables, USERS and SCORE. USERS can be of three types, administrators, supervisors, trainers. Each trainer is assigned to a supervisor, which is stored in the users table.
    Now the issue is: A supervisor can view his scores and the scores for trainers assigned to him.
    The query I have written is :

    Code:
    SELECT s.* FROM score as s, users as u WHERE s.trainerid=u.userid AND u.supervisor=50
    UNION
    SELECT * FROM score WHERE trainerid=50;
    Is there a better way of achieving the result?
    I am using MySQL 5.0.
    Thank you.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Optimization of query

    I don't know if it will be any faster (I suspect it might be), but I think this is at least easier to read:
    Code:
    SELECT s.* 
    FROM score as s
    LEFT JOIN users as u ON (s.trainerid=u.userid) 
    WHERE s.trainerid=50 OR u.supervisor=50;
    ..note that using .* in the select list is slower than specifying the fields.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Re: Optimization of query

    Thank you. That is definitely easier to read. I need all the fields from the score table.

Posting Permissions

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



Click Here to Expand Forum to Full Width