PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > Speeding up large query
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Speeding up large query

Réponse
 
LinkBack Outils de la discussion
Vieux 04/10/2007, 14h11   #1
nk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Speeding up large query

I have two tables with approximately 5 mio rows each, on which I'm
trying to execute the following query:

Code:
SELECT field1
FROM main_table USE INDEX (sort_field)
LEFT JOIN second_table l USING (primary_key_field)
WHERE l.field2 = "abcde"
ORDER BY sort_field ASC LIMIT 5000, 3000
I already optimized the query by adding "USE INDEX (sort_field)" to
avoid temporary tables for sorting, but when I execute it, it still
locks the table for around 10 minutes. In the process list the state
of the query is 'Sending data', but I doubt that the data transfer is
actually what takes so long, since both servers have a pretty fast
connection.

Any ideas what I could change to speed this query up? I assume the
biggest problem right now is the WHERE condition, since it doesn't use
an index, but I don't know how I could fix that without losing the
index on sort_field.

  Réponse avec citation
Vieux 04/10/2007, 14h58   #2
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Speeding up large query

On 4 Oct, 14:11, nk <nkoell...@gmail.com> wrote:
> I have two tables with approximately 5 mio rows each, on which I'm
> trying to execute the following query:
>
>
Code:
SELECT field1
>         FROM main_table USE INDEX (sort_field)
>         LEFT JOIN second_table l USING (primary_key_field)
>         WHERE l.field2 = "abcde"
>         ORDER BY sort_field ASC LIMIT 5000, 3000
>
> I already optimized the query by adding "USE INDEX (sort_field)" to
> avoid temporary tables for sorting, but when I execute it, it still
> locks the table for around 10 minutes. In the process list the state
> of the query is 'Sending data', but I doubt that the data transfer is
> actually what takes so long, since both servers have a pretty fast
> connection.
>
> Any ideas what I could change to speed this query up? I assume the
> biggest problem right now is the WHERE condition, since it doesn't use
> an index, but I don't know how I could fix that without losing the
> index on sort_field.


Well,
since your where clause operates on field2 of second_table, could you
swap the tables round and use an index on field2 to limit the rows to
be operated on?

As it is, all the rows in main_table have to be read, a JOIN attempted
into second_table and, if the JOIN is successful, field2 must be
examined.

  Réponse avec citation
Vieux 04/10/2007, 15h27   #3
nk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Speeding up large query

On Oct 4, 3:58 pm, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> Well,
> since your where clause operates on field2 of second_table, could you
> swap the tables round and use an index on field2 to limit the rows to
> be operated on?
>
> As it is, all the rows in main_table have to be read, a JOIN attempted
> into second_table and, if the JOIN is successful, field2 must be
> examined.

Wouldn't this prevent me from using the index on sort_field, though?

  Réponse avec citation
Vieux 04/10/2007, 15h44   #4
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Speeding up large query

On 4 Oct, 15:27, nk <nkoell...@gmail.com> wrote:
> On Oct 4, 3:58 pm, Captain Paralytic <paul_laut...@yahoo.com> wrote:> Well,
> > since your where clause operates on field2 of second_table, could you
> > swap the tables round and use an index on field2 to limit the rows to
> > be operated on?

>
> > As it is, all the rows in main_table have to be read, a JOIN attempted
> > into second_table and, if the JOIN is successful, field2 must be
> > examined.

>
> Wouldn't this prevent me from using the index on sort_field, though?


Yes, but I don't know what the data looks like. It may be quicker to
do a filesort than to have to read all the records in main_table.

How many records in second_table have l.field2 = "abcde"?

  Réponse avec citation
Vieux 04/10/2007, 16h01   #5
nk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Speeding up large query

On Oct 4, 4:44 pm, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 4 Oct, 15:27, nk <nkoell...@gmail.com> wrote:
>
> > On Oct 4, 3:58 pm, Captain Paralytic <paul_laut...@yahoo.com> wrote:> Well,
> > > since your where clause operates on field2 of second_table, could you
> > > swap the tables round and use an index on field2 to limit the rows to
> > > be operated on?

>
> > > As it is, all the rows in main_table have to be read, a JOIN attempted
> > > into second_table and, if the JOIN is successful, field2 must be
> > > examined.

>
> > Wouldn't this prevent me from using the index on sort_field, though?

>
> Yes, but I don't know what the data looks like. It may be quicker to
> do a filesort than to have to read all the records in main_table.
>
> How many records in second_table have l.field2 = "abcde"?

Around 1 million.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h38.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10320 seconds with 13 queries