tags:

views:

84

answers:

4

I have the following query:

SELECT `masters_tp`.*, `masters_cp`.`cp` as cp, `masters_cp`.`punti` as punti
FROM (`masters_tp`)
LEFT JOIN `masters_cp` ON `masters_cp`.`nickname` = `masters_tp`.`nickname`
WHERE `masters_tp`.`stake` = 'report_A'
AND `masters_cp`.`stake` = 'report_A'
ORDER BY `masters_tp`.`tp` DESC, `masters_cp`.`punti` DESC
LIMIT 400;

Is there something wrong with this query that could affect the server memory?

Here is the output of EXPLAIN

| id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows  | Extra                                        |
+----+-------------+------------+------+---------------+------+---------+------+-------+----------------------------------------------+
|  1 | SIMPLE      | masters_cp | ALL  | NULL          | NULL |    NULL | NULL |  8943 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | masters_tp | ALL  | NULL          | NULL |    NULL | NULL | 12693 | Using where                                  |
+2  A: 

It is simple query. I think everything is ok with it. You can try add indexes on 'stake' fields or make limit lower.

Pawka
+6  A: 

Run the same query prefixed with EXPLAIN and add the output to your question - this will show what indexes you are using and the number of rows being analyzed.

You can see from your explain that no indexes are being used, and its having to look at thousands of rows to get your result. Try adding an index on the columns used to perform the join, e.g. nickname and stake:

ALTER TABLE masters_tp ADD INDEX(nickname),ADD INDEX(stake);
ALTER TABLE masters_cp ADD INDEX(nickname),ADD INDEX(stake);

(I've assumed the columns might have duplicated values, if not, use UNIQUE rather than INDEX). See the MySQL manual for more information.

Paul Dixon
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+------------+------+---------------+------+---------+------+-------+----------------------------------------------+| 1 | SIMPLE | masters_cp | ALL | NULL | NULL | NULL | NULL | 8943 | Using where; Using temporary; Using filesort || 1 | SIMPLE | masters_tp | ALL | NULL | NULL | NULL | NULL | 12693 | Using where |
insic
oppsformatting is a train wreck i will post again i will reformat the result
insic
just edit your question
Paul Dixon
there, did it for you :)
Paul Dixon
result on EXPLAIN id = 1select_type = SIMPLEtable = masters_cptype = ALLpossible_keys = NULLkey = NULLkey_len = NULLref = nullrows = 8943extra = Using where; Using temporary; Using filesortid = 1select_type = SIMPLEtable = masters_tptype = ALLpossible_keys = NULLkey = NULLkey_len = NULLref = NULLrows = 12693 extra = Using where
insic
thanks for the help, im really slow right now I cant rewrite the query. and besides I need to read on how to index for joins with MySQL
insic
+5  A: 

Replace the "masters_tp.* " bit by explicitly naming only the fields from that table you actually need. Even if you need them all, name them all.

CodeByMoonlight
thanks i will do it.
insic
+5  A: 

There's actually no reason to do a left join here. You're using your filters to whisk away any leftiness of the join. Try this:

SELECT 
    `masters_tp`.*, 
    `masters_cp`.`cp` as cp, 
    `masters_cp`.`punti` as punti
FROM 
    `masters_tp`
    INNER JOIN `masters_cp` ON 
        `masters_tp`.`stake` = `masters_cp`.stake`
        and `masters_tp`.`nickname` = `masters_cp`.`nickname`
WHERE 
    `masters_tp`.`stake` = 'report_A'
ORDER BY 
    `masters_tp`.`tp` DESC, 
    `masters_cp`.`punti` DESC
LIMIT 400;

inner joins tend to be faster than left joins. The query can limit the number of rows that have to be joined using the predicates (aka the where clause). This means that the database is handling, potentially, a lot less rows, which obviously speeds things up.

Additionally, make sure you have a non-clustered index on stake and nickname (in that order).

Eric
He might actually want the left join the key is moving that conditon from the where clasue to the join. You are right that right now, it is not returning a left join due to a code bug.
HLGEM