Fixes for PostgreSQL full text search

- Drop individual GIN indices that aren't used by the search query.
- Add missing indices to speed up view query (missing dossier_id)
- Fix view query for drop_down_lists (and merge it with champs)
This commit is contained in:
Julien Portalier 2016-10-28 19:34:29 +02:00
parent 0ea69cfc3e
commit 6bb1ad892f
5 changed files with 2104 additions and 483 deletions

View file

@ -12,11 +12,12 @@ class Search < ActiveRecord::Base
def results
if @query.present?
self.class
.select("DISTINCT(dossiers.*)")
.select("DISTINCT(searches.dossier_id)")
.search(@query)
.joins(:dossier)
.where(dossier_id: @gestionnaire.dossier_ids)
.where("dossiers.archived = ? AND dossiers.state != ?", false, "draft")
.preload(:dossier)
.map(&:dossier)
else
Search.none
@ -31,9 +32,10 @@ class Search < ActiveRecord::Base
%i(term)
end
# NOTE: could be executed concurrently
# See https://github.com/thoughtbot/scenic#what-about-materialized-views
# Refreshes the materialized searches view.
def self.refresh
# NOTE: could be executed concurrently
# See https://github.com/thoughtbot/scenic#what-about-materialized-views
Scenic.database.refresh_materialized_view(table_name, concurrently: false)
end
end

View file

@ -1,68 +1,27 @@
class CreateSearches < ActiveRecord::Migration
def up
add_index :champs, :dossier_id
add_index :champs, :type_de_champ_id
add_index :drop_down_lists, :type_de_champ_id
add_index :etablissements, :dossier_id
add_index :entreprises, :dossier_id
add_index :france_connect_informations, :user_id
add_index :individuals, :dossier_id
add_index :pieces_justificatives, :dossier_id
add_index :rna_informations, :entreprise_id
create_view :searches, materialized: true
matrix.each do |table, fields|
fields.each do |field|
execute "CREATE INDEX tsv_index_#{table}_on_#{field} ON #{table} USING GIN(to_tsvector('french', #{field}))"
end
end
end
def down
remove_index :champs, :dossier_id
remove_index :champs, :type_de_champ_id
remove_index :drop_down_lists, :type_de_champ_id
remove_index :etablissements, :dossier_id
remove_index :entreprises, :dossier_id
remove_index :france_connect_informations, :user_id
remove_index :individuals, :dossier_id
remove_index :pieces_justificatives, :dossier_id
remove_index :rna_informations, :entreprise_id
drop_view :searches
matrix.each do |table, fields|
fields.each do |field|
execute "DROP INDEX IF EXISTS tsv_index_#{table}_on_#{field}"
end
end
end
def matrix
{
cerfas: %i(
content
),
champs: %i(
value
),
entreprises: %i(
siren
numero_tva_intracommunautaire
forme_juridique
forme_juridique_code
nom_commercial
raison_sociale
siret_siege_social
nom
prenom
),
rna_informations: %i(
association_id
titre
objet
),
etablissements: %i(
siret
naf
libelle_naf
adresse
code_postal
localite
code_insee_localite
),
individuals: %i(
nom
prenom
),
pieces_justificatives: %i(
content
),
france_connect_informations: %i(
given_name
family_name
),
}
end
end

View file

@ -1,414 +0,0 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161025150900) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "administrateurs", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "api_token"
end
add_index "administrateurs", ["email"], name: "index_administrateurs_on_email", unique: true, using: :btree
add_index "administrateurs", ["reset_password_token"], name: "index_administrateurs_on_reset_password_token", unique: true, using: :btree
create_table "administrateurs_gestionnaires", id: false, force: :cascade do |t|
t.integer "administrateur_id"
t.integer "gestionnaire_id"
end
add_index "administrateurs_gestionnaires", ["administrateur_id"], name: "index_administrateurs_gestionnaires_on_administrateur_id", using: :btree
add_index "administrateurs_gestionnaires", ["gestionnaire_id", "administrateur_id"], name: "unique_couple_administrateur_gestionnaire", unique: true, using: :btree
add_index "administrateurs_gestionnaires", ["gestionnaire_id"], name: "index_administrateurs_gestionnaires_on_gestionnaire_id", using: :btree
create_table "administrations", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "administrations", ["email"], name: "index_administrations_on_email", unique: true, using: :btree
add_index "administrations", ["reset_password_token"], name: "index_administrations_on_reset_password_token", unique: true, using: :btree
create_table "assign_tos", id: false, force: :cascade do |t|
t.integer "gestionnaire_id"
t.integer "procedure_id"
end
add_index "assign_tos", ["gestionnaire_id"], name: "index_assign_tos_on_gestionnaire_id", using: :btree
add_index "assign_tos", ["procedure_id"], name: "index_assign_tos_on_procedure_id", using: :btree
create_table "cadastres", force: :cascade do |t|
t.string "surface_intersection"
t.float "surface_parcelle"
t.string "numero"
t.integer "feuille"
t.string "section"
t.string "code_dep"
t.string "nom_com"
t.string "code_com"
t.string "code_arr"
t.text "geometry"
t.integer "dossier_id"
end
create_table "cerfas", force: :cascade do |t|
t.string "content"
t.integer "dossier_id"
t.datetime "created_at"
t.integer "user_id"
t.string "original_filename"
t.string "content_secure_token"
end
add_index "cerfas", ["dossier_id"], name: "index_cerfas_on_dossier_id", using: :btree
create_table "champs", force: :cascade do |t|
t.string "value"
t.integer "type_de_champ_id"
t.integer "dossier_id"
t.string "type"
end
create_table "commentaires", force: :cascade do |t|
t.string "email"
t.datetime "created_at", null: false
t.string "body"
t.integer "dossier_id"
t.datetime "updated_at", null: false
t.integer "piece_justificative_id"
end
add_index "commentaires", ["dossier_id"], name: "index_commentaires_on_dossier_id", using: :btree
create_table "dossiers", force: :cascade do |t|
t.boolean "autorisation_donnees"
t.string "nom_projet"
t.integer "procedure_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "state"
t.integer "user_id"
t.text "json_latlngs"
t.boolean "archived", default: false
t.boolean "mandataire_social", default: false
t.datetime "deposit_datetime"
end
add_index "dossiers", ["procedure_id"], name: "index_dossiers_on_procedure_id", using: :btree
add_index "dossiers", ["user_id"], name: "index_dossiers_on_user_id", using: :btree
create_table "drop_down_lists", force: :cascade do |t|
t.string "value"
t.integer "type_de_champ_id"
end
create_table "entreprises", force: :cascade do |t|
t.string "siren"
t.integer "capital_social"
t.string "numero_tva_intracommunautaire"
t.string "forme_juridique"
t.string "forme_juridique_code"
t.string "nom_commercial"
t.string "raison_sociale"
t.string "siret_siege_social"
t.string "code_effectif_entreprise"
t.datetime "date_creation"
t.string "nom"
t.string "prenom"
t.integer "dossier_id"
end
create_table "etablissements", force: :cascade do |t|
t.string "siret"
t.boolean "siege_social"
t.string "naf"
t.string "libelle_naf"
t.string "adresse"
t.string "numero_voie"
t.string "type_voie"
t.string "nom_voie"
t.string "complement_adresse"
t.string "code_postal"
t.string "localite"
t.string "code_insee_localite"
t.integer "dossier_id"
t.integer "entreprise_id"
end
create_table "exercices", force: :cascade do |t|
t.string "ca"
t.datetime "dateFinExercice"
t.integer "date_fin_exercice_timestamp"
t.integer "etablissement_id"
end
create_table "follows", force: :cascade do |t|
t.integer "gestionnaire_id"
t.integer "dossier_id"
end
add_index "follows", ["dossier_id"], name: "index_follows_on_dossier_id", using: :btree
add_index "follows", ["gestionnaire_id"], name: "index_follows_on_gestionnaire_id", using: :btree
create_table "france_connect_informations", force: :cascade do |t|
t.string "gender"
t.string "given_name"
t.string "family_name"
t.date "birthdate"
t.string "birthplace"
t.string "france_connect_particulier_id"
t.integer "user_id"
t.string "email_france_connect"
end
create_table "gestionnaires", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "procedure_filter"
end
add_index "gestionnaires", ["email"], name: "index_gestionnaires_on_email", unique: true, using: :btree
add_index "gestionnaires", ["reset_password_token"], name: "index_gestionnaires_on_reset_password_token", unique: true, using: :btree
create_table "individuals", force: :cascade do |t|
t.string "nom"
t.string "prenom"
t.string "birthdate"
t.integer "dossier_id"
t.string "gender"
end
create_table "invites", force: :cascade do |t|
t.string "email"
t.string "email_sender"
t.integer "dossier_id"
t.integer "user_id"
t.string "type", default: "InviteGestionnaire"
end
create_table "mail_templates", force: :cascade do |t|
t.string "object"
t.text "body"
t.string "type"
t.integer "procedure_id"
end
create_table "module_api_cartos", force: :cascade do |t|
t.integer "procedure_id"
t.boolean "use_api_carto", default: false
t.boolean "quartiers_prioritaires", default: false
t.boolean "cadastre", default: false
end
add_index "module_api_cartos", ["procedure_id"], name: "index_module_api_cartos_on_procedure_id", unique: true, using: :btree
create_table "pieces_justificatives", force: :cascade do |t|
t.string "content"
t.integer "dossier_id"
t.integer "type_de_piece_justificative_id"
t.datetime "created_at"
t.integer "user_id"
t.string "original_filename"
t.string "content_secure_token"
end
add_index "pieces_justificatives", ["type_de_piece_justificative_id"], name: "index_pieces_justificatives_on_type_de_piece_justificative_id", using: :btree
create_table "preference_list_dossiers", force: :cascade do |t|
t.string "libelle"
t.string "table"
t.string "attr"
t.string "attr_decorate"
t.string "bootstrap_lg"
t.string "order"
t.string "filter"
t.integer "gestionnaire_id"
t.integer "procedure_id"
end
create_table "preference_smart_listing_pages", force: :cascade do |t|
t.string "liste"
t.integer "page"
t.integer "procedure_id"
t.integer "gestionnaire_id"
end
create_table "procedure_paths", force: :cascade do |t|
t.string "path", limit: 30
t.integer "procedure_id"
t.integer "administrateur_id"
end
add_index "procedure_paths", ["path"], name: "index_procedure_paths_on_path", using: :btree
create_table "procedures", force: :cascade do |t|
t.string "libelle"
t.string "description"
t.string "organisation"
t.string "direction"
t.string "lien_demarche"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "test"
t.integer "administrateur_id"
t.boolean "archived", default: false
t.boolean "euro_flag", default: false
t.string "logo"
t.boolean "cerfa_flag", default: false
t.string "logo_secure_token"
t.boolean "published", default: false, null: false
t.string "lien_site_web"
t.string "lien_notice"
t.boolean "for_individual", default: false
t.boolean "individual_with_siret", default: false
end
create_table "quartier_prioritaires", force: :cascade do |t|
t.string "code"
t.string "nom"
t.string "commune"
t.text "geometry"
t.integer "dossier_id"
end
create_table "rna_informations", force: :cascade do |t|
t.string "association_id"
t.string "titre"
t.text "objet"
t.date "date_creation"
t.date "date_declaration"
t.date "date_publication"
t.integer "entreprise_id"
end
create_table "types_de_champ", force: :cascade do |t|
t.string "libelle"
t.string "type_champ"
t.integer "order_place"
t.integer "procedure_id"
t.text "description"
t.boolean "mandatory", default: false
t.string "type"
end
create_table "types_de_piece_justificative", force: :cascade do |t|
t.string "libelle"
t.string "description"
t.boolean "api_entreprise", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "procedure_id"
t.integer "order_place"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "siret"
t.string "loged_in_with_france_connect", default: "false"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
add_foreign_key "cerfas", "dossiers"
add_foreign_key "commentaires", "dossiers"
add_foreign_key "dossiers", "users"
add_foreign_key "procedure_paths", "administrateurs"
add_foreign_key "procedure_paths", "procedures"
create_view :searches, sql_definition: <<-SQL
SELECT dossiers.id AS dossier_id,
(dossiers.id)::text AS term
FROM dossiers
UNION
SELECT cerfas.dossier_id,
cerfas.content AS term
FROM cerfas
UNION
SELECT champs.dossier_id,
champs.value AS term
FROM champs
UNION
SELECT champs.dossier_id,
drop_down_lists.value AS term
FROM (drop_down_lists
JOIN champs ON ((champs.type_de_champ_id = champs.type_de_champ_id)))
UNION
SELECT entreprises.dossier_id,
(((((((((((((((((((((((entreprises.siren)::text || ' '::text) || (entreprises.numero_tva_intracommunautaire)::text) || ' '::text) || (entreprises.forme_juridique)::text) || ' '::text) || (entreprises.forme_juridique_code)::text) || ' '::text) || (entreprises.nom_commercial)::text) || ' '::text) || (entreprises.raison_sociale)::text) || ' '::text) || (entreprises.siret_siege_social)::text) || ' '::text) || (entreprises.nom)::text) || ' '::text) || (entreprises.prenom)::text) || ' '::text) || (rna_informations.association_id)::text) || ' '::text) || (rna_informations.titre)::text) || ' '::text) || rna_informations.objet) AS term
FROM (entreprises
JOIN rna_informations ON ((rna_informations.entreprise_id = entreprises.id)))
UNION
SELECT etablissements.dossier_id,
(((((((((((((etablissements.siret)::text || ' '::text) || (etablissements.naf)::text) || ' '::text) || (etablissements.libelle_naf)::text) || ' '::text) || (etablissements.adresse)::text) || ' '::text) || (etablissements.code_postal)::text) || ' '::text) || (etablissements.localite)::text) || ' '::text) || (etablissements.code_insee_localite)::text) AS term
FROM etablissements
UNION
SELECT individuals.dossier_id,
(((individuals.nom)::text || ' '::text) || (individuals.prenom)::text) AS term
FROM individuals
UNION
SELECT pieces_justificatives.dossier_id,
pieces_justificatives.content AS term
FROM pieces_justificatives
UNION
SELECT dossiers.id AS dossier_id,
(((france_connect_informations.given_name)::text || ' '::text) || (france_connect_informations.family_name)::text) AS term
FROM (france_connect_informations
JOIN dossiers ON ((dossiers.user_id = france_connect_informations.user_id)));
SQL
end

2077
db/structure.sql Normal file

File diff suppressed because it is too large Load diff

View file

@ -7,13 +7,10 @@ UNION SELECT cerfas.dossier_id,
FROM cerfas
UNION SELECT champs.dossier_id,
champs.value AS term
FROM champs
UNION SELECT champs.dossier_id,
champs.value || ' ' ||
drop_down_lists.value AS term
FROM drop_down_lists
INNER JOIN champs ON champs.type_de_champ_id = champs.type_de_champ_id
FROM champs
INNER JOIN drop_down_lists ON drop_down_lists.type_de_champ_id = champs.type_de_champ_id
UNION SELECT entreprises.dossier_id,
entreprises.siren || ' ' ||
@ -43,7 +40,7 @@ UNION SELECT etablissements.dossier_id,
UNION SELECT individuals.dossier_id,
individuals.nom || ' ' ||
individuals.prenom AS term
individuals.prenom AS term
FROM individuals
UNION SELECT pieces_justificatives.dossier_id,