Giter Club home page Giter Club logo

Comments (2)

mathieubossaert avatar mathieubossaert commented on August 23, 2024

Après lecture de divers posts relatifs à la question, voici une méthode efficace, utilisant les fonctions de fenêtre dans une CTE.
Le principe est simple, on attribue un rang des "id_obs" selon les critères et l'ordre demandé. On sélectionne toutes les données dont l'ordre est supérieur à "OFFSET" en limitant le nombre de ligne à "LIMIT". L'exemple ci-dessous (100 lignes à partir de la 100000ème) prend 250ms contre 65 secondes !

WITH tri AS (
    SELECT row_number() over (order by id_obs desc), id_obs  
    FROM saisie.saisie_observation
    LEFT JOIN md.etude using(id_etude) left JOIN md.protocole USING(id_protocole) 
    LEFT JOIN md.personne AS numerisateurs ON numerisateur = numerisateurs.id_personne 
    LEFT JOIN md.personne AS validateurs ON validateur = validateurs.id_personne 
    LEFT JOIN ign_bd_topo.lieu_dit ON id_lieu_dit = ign_bd_topo.lieu_dit.id 
    LEFT JOIN ign_bd_topo.commune ON ign_bd_topo.commune.code_insee = saisie.saisie_observation.code_insee
    LEFT JOIN inpn.taxref USING(cd_nom)
    )
SELECT 
    st_asgeojson(st_transform(saisie.saisie_observation.geometrie,
    4326)), id_obs, heure_obs, date_obs, date_debut_obs, date_fin_obs, date_textuelle,
    saisie_observation.regne, saisie_observation.nom_vern, saisie_observation.nom_complet,
    cd_nom, phylum, classe, ordre, famille, nom_valide, st_geometrytype(saisie.saisie_observation
    .geometrie),effectif, effectif_min, effectif_max, effectif_textuel, type_effectif, phenologie,
    precision, determination, id_waypoint, depart, latitude, elevation, ign_bd_topo.commune.code_insee,
    substring(ign_bd_topo.commune.code_insee from 1 for 2) AS dep, ign_bd_topo.commune.nom AS commune,
    id_lieu_dit, ign_bd_topo.lieu_dit.nom AS lieu_dit, observateur, md.liste_nom_auteur(observateur
    ) AS observat, validateur, structure, md.liste_nom_structure(structure) AS struct, localisation, id_etude, nom_etude, id_protocole, libelle,
    diffusable, remarque_obs, statut_validation, decision_validation, url_photo, (numerisateurs.nom || ' ' || numerisateurs.prenom) AS numerisat, longitude,
    commentaire_photo, (validateurs.nom || ' ' || validateurs.prenom) AS validat
FROM saisie.saisie_observation join tri USING(id_obs)
LEFT JOIN md.etude using(id_etude) left JOIN md.protocole USING(id_protocole) 
LEFT JOIN md.personne AS numerisateurs ON numerisateur = numerisateurs.id_personne 
LEFT JOIN md.personne AS validateurs ON validateur = validateurs.id_personne 
LEFT JOIN ign_bd_topo.lieu_dit ON id_lieu_dit = ign_bd_topo.lieu_dit.id 
LEFT JOIN ign_bd_topo.commune ON ign_bd_topo.commune.code_insee = saisie.saisie_observation.code_insee
LEFT JOIN inpn.taxref USING(cd_nom)
WHERE row_number>100000 LIMIT 100

http://use-the-index-luke.com/no-offset
http://leopard.in.ua/2014/10/11/postgresql-paginattion/

from obs_occ.

amandine-sahl avatar amandine-sahl commented on August 23, 2024

cf commit : dcccd55

from obs_occ.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.