Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Limite prático para o comprimento da consulta SQL (especificamente MySQL)


Ler sua pergunta me faz querer jogar um RPG.

Isso definitivamente não é muito longo. Desde que estejam bem formatados, diria que um limite prático é de cerca de 100 linhas. Depois disso, é melhor dividir as subconsultas em visualizações apenas para evitar que seus olhos se cruzem.

Eu trabalhei com algumas consultas com mais de 1.000 linhas, e isso é difícil de depurar.

A propósito, posso sugerir uma versão reformatada? Isso é principalmente para demonstrar a importância da formatação; Acredito que isso será mais fácil de entender.
select *  
from
  4e_magic_items mi
 ,4e_magic_item_levels mil
 ,4e_monster_sources ms
where mi.id = mil.itemid
  and mi.source = ms.id
  and itemlevel between 1 and 30
  and source not in(16,2,5,13,15,3,4,12,7,14,11,10,8,1,6,9)  
  and type not in(
                  'Arms' ,'Feet' ,'Hands' ,'Head' ,'Neck' ,'Orb' ,
                  'Potion' ,'Ring' ,'Rod' ,'Staff' ,'Symbol' ,'Waist' ,
                  'Wand' ,'Wondrous Item' ,'Alchemical Item' ,'Elixir' ,
                  'Reagent' ,'Whetstone' ,'Other Consumable' ,'Companion' ,
                  'Mount'
                 )
  and ((type != 'Armor') or (false))
  and ((type != 'Weapon') or (false))
order by
  type asc
 ,itemlevel asc
 ,name asc

/*
Some thoughts:
==============
0 - Formatting really matters, in SQL even more than most languages.
1 - consider selecting only the columns you need, not "*"
2 - use of table aliases makes it short & clear ("MI", "MIL" in my example)
3 - joins in the WHERE clause will un-clutter your FROM clause
4 - use NOT IN for long lists
5 - logically, the last two lines can be added to the "type not in" section.
    I'm not sure why you have the "or false", but I'll assume some good reason
    and leave them here.
*/