Estoy intentando realizar un buscador en PHP+SQL con filtros. Estoy elaborando la consulta que me saque los totales de todos los filtros de un golpe, pero esta consulta me tarda una eternidad.
¿Alguien conoce algún método más eficaz para realizarlo?
Esta es la mega consulta:
Código SQL:
Ver original
SELECT COUNT(*) AS total_sacados, SUM(CASE WHEN fecha >=1444317968 THEN 1 ELSE 0 END) AS fecha_7, SUM(CASE WHEN fecha >=1442330768 THEN 1 ELSE 0 END) AS fecha_30, SUM(CASE WHEN fecha >=1429370768 THEN 1 ELSE 0 END) AS fecha_180, SUM(CASE WHEN fecha >=1413386768 THEN 1 ELSE 0 END) AS fecha_365, SUM(CASE WHEN fecha >=1381850768 THEN 1 ELSE 0 END) AS fecha_730, SUM(CASE WHEN usuarios.nacimiento < 877274768 AND usuarios.nacimiento >= 719594768 THEN 1 ELSE 0 END) AS nacimiento0, SUM(CASE WHEN usuarios.nacimiento < 719594768 AND usuarios.nacimiento >= 593450768 THEN 1 ELSE 0 END) AS nacimiento1 , SUM(CASE WHEN usuarios.nacimiento < 593450768 AND usuarios.nacimiento >= 467306768 THEN 1 ELSE 0 END) AS nacimiento2 , SUM(CASE WHEN usuarios.nacimiento < 467306768 AND usuarios.nacimiento >= 278090768 THEN 1 ELSE 0 END) AS nacimiento3 , SUM(CASE WHEN usuarios.nacimiento < 278090768 AND usuarios.nacimiento >= 88874768 THEN 1 ELSE 0 END) AS nacimiento4 , SUM(CASE WHEN usuarios.nacimiento < 88874768 THEN 1 ELSE 0 END) AS nacimiento5 , SUM(CASE WHEN datos.experiencia >= 0 AND datos.experiencia < (2*365) THEN 1 ELSE 0 END) AS experiencia0 , SUM(CASE WHEN datos.experiencia >= (2*365) AND datos.experiencia < (4*365) THEN 1 ELSE 0 END) AS experiencia1 , SUM(CASE WHEN datos.experiencia >= (4*365) AND datos.experiencia < (6*365) THEN 1 ELSE 0 END) AS experiencia2 , SUM(CASE WHEN datos.experiencia >= (6*365) AND datos.experiencia < (11*365) THEN 1 ELSE 0 END) AS experiencia3 , SUM(CASE WHEN datos.experiencia >= (11*365) AND datos.experiencia < (16*365) THEN 1 ELSE 0 END) AS experiencia4 , SUM(CASE WHEN datos.experiencia >= (16*365) THEN 1 ELSE 0 END) AS experiencia5 , SUM(CASE WHEN estudi=14 THEN 1 ELSE 0 END) AS estudios_14 , SUM(CASE WHEN estudi=3 THEN 1 ELSE 0 END) AS estudios_3 , SUM(CASE WHEN estudi=5 THEN 1 ELSE 0 END) AS estudios_5 , SUM(CASE WHEN estudi=4 THEN 1 ELSE 0 END) AS estudios_4 , SUM(CASE WHEN estudi=6 THEN 1 ELSE 0 END) AS estudios_6 , SUM(CASE WHEN estudi=7 THEN 1 ELSE 0 END) AS estudios_7 , SUM(CASE WHEN estudi=2 THEN 1 ELSE 0 END) AS estudios_2 , SUM(CASE WHEN estudi=15 THEN 1 ELSE 0 END) AS estudios_15 , SUM(CASE WHEN estudi=8 THEN 1 ELSE 0 END) AS estudios_8 , SUM(CASE WHEN estudi=1 THEN 1 ELSE 0 END) AS estudios_1 , SUM(CASE WHEN estudi=16 THEN 1 ELSE 0 END) AS estudios_16 , SUM(CASE WHEN estudi=10 THEN 1 ELSE 0 END) AS estudios_10 , SUM(CASE WHEN estudi=9 THEN 1 ELSE 0 END) AS estudios_9 , SUM(CASE WHEN estudi=12 THEN 1 ELSE 0 END) AS estudios_12 , SUM(CASE WHEN estudi=13 THEN 1 ELSE 0 END) AS estudios_13 , SUM(CASE WHEN estudi=17 THEN 1 ELSE 0 END) AS estudios_17 , SUM(CASE WHEN estudi=18 THEN 1 ELSE 0 END) AS estudios_18 , SUM(CASE WHEN especialidad LIKE '%,45,%' THEN 1 ELSE 0 END) AS funcion_45 , SUM(CASE WHEN especialidad LIKE '%,50,%' THEN 1 ELSE 0 END) AS funcion_50 , SUM(CASE WHEN especialidad LIKE '%,32,%' THEN 1 ELSE 0 END) AS funcion_32 , SUM(CASE WHEN especialidad LIKE '%,37,%' THEN 1 ELSE 0 END) AS funcion_37 , SUM(CASE WHEN especialidad LIKE '%,51,%' THEN 1 ELSE 0 END) AS funcion_51 , SUM(CASE WHEN especialidad LIKE '%,30,%' THEN 1 ELSE 0 END) AS funcion_30 , SUM(CASE WHEN especialidad LIKE '%,29,%' THEN 1 ELSE 0 END) AS funcion_29 , SUM(CASE WHEN especialidad LIKE '%,28,%' THEN 1 ELSE 0 END) AS funcion_28 , SUM(CASE WHEN especialidad LIKE '%,27,%' THEN 1 ELSE 0 END) AS funcion_27 , SUM(CASE WHEN especialidad LIKE '%,24,%' THEN 1 ELSE 0 END) AS funcion_24 , SUM(CASE WHEN especialidad LIKE '%,21,%' THEN 1 ELSE 0 END) AS funcion_21 , SUM(CASE WHEN especialidad LIKE '%,19,%' THEN 1 ELSE 0 END) AS funcion_19 , SUM(CASE WHEN especialidad LIKE '%,18,%' THEN 1 ELSE 0 END) AS funcion_18 , SUM(CASE WHEN especialidad LIKE '%,25,%' THEN 1 ELSE 0 END) AS funcion_25 , SUM(CASE WHEN especialidad LIKE '%,38,%' THEN 1 ELSE 0 END) AS funcion_38 , SUM(CASE WHEN especialidad LIKE '%,46,%' THEN 1 ELSE 0 END) AS funcion_46 , SUM(CASE WHEN especialidad LIKE '%,12,%' THEN 1 ELSE 0 END) AS funcion_12 , SUM(CASE WHEN especialidad LIKE '%,47,%' THEN 1 ELSE 0 END) AS funcion_47 , SUM(CASE WHEN especialidad LIKE '%,39,%' THEN 1 ELSE 0 END) AS funcion_39 , SUM(CASE WHEN especialidad LIKE '%,40,%' THEN 1 ELSE 0 END) AS funcion_40 , SUM(CASE WHEN especialidad LIKE '%,48,%' THEN 1 ELSE 0 END) AS funcion_48 , SUM(CASE WHEN especialidad LIKE '%,9,%' THEN 1 ELSE 0 END) AS funcion_9 , SUM(CASE WHEN especialidad LIKE '%,8,%' THEN 1 ELSE 0 END) AS funcion_8 , SUM(CASE WHEN especialidad LIKE '%,41,%' THEN 1 ELSE 0 END) AS funcion_41 , SUM(CASE WHEN especialidad LIKE '%,49,%' THEN 1 ELSE 0 END) AS funcion_49 , SUM(CASE WHEN especialidad LIKE '%,42,%' THEN 1 ELSE 0 END) AS funcion_42 , SUM(CASE WHEN especialidad LIKE '%,43,%' THEN 1 ELSE 0 END) AS funcion_43 , SUM(CASE WHEN especialidad LIKE '%,3,%' THEN 1 ELSE 0 END) AS funcion_3 , SUM(CASE WHEN especialidad LIKE '%,2,%' THEN 1 ELSE 0 END) AS funcion_2 , SUM(CASE WHEN especialidad LIKE '%,44,%' THEN 1 ELSE 0 END) AS funcion_44 , SUM(CASE WHEN situacion = 0 THEN 1 ELSE 0 END) AS situacion0 , SUM(CASE WHEN situacion = 1 THEN 1 ELSE 0 END) AS situacion1 , SUM(CASE WHEN situacion = 2 THEN 1 ELSE 0 END) AS situacion2 , SUM(CASE WHEN situacion = 3 THEN 1 ELSE 0 END) AS situacion3 , SUM(CASE WHEN contra = 1 THEN 1 ELSE 0 END) AS contra_1 , SUM(CASE WHEN contra = 2 THEN 1 ELSE 0 END) AS contra_2 , SUM(CASE WHEN contra = 3 THEN 1 ELSE 0 END) AS contra_3 , SUM(CASE WHEN contra = 4 THEN 1 ELSE 0 END) AS contra_4 , SUM(CASE WHEN contra = 5 THEN 1 ELSE 0 END) AS contra_5 , SUM(CASE WHEN contra = 6 THEN 1 ELSE 0 END) AS contra_6 , SUM(CASE WHEN contra = 7 THEN 1 ELSE 0 END) AS contra_7 , SUM(CASE WHEN dispo = 'si' THEN 1 ELSE 0 END) AS dispo_si , SUM(CASE WHEN dispo = 'no' THEN 1 ELSE 0 END) AS dispo_no FROM datos (nolock), usuarios (nolock) WHERE usuarios.id=datos.id AND CONTAINS (texto_completo,'pepito')
Muchas gracias!