Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7642197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:07:42+00:00 2026-05-31T09:07:42+00:00

I have a query that make some joins. SELECT feedback.note as notes, to_char(feedback.data_compilazione,’DD/MM/YYYY’) as

  • 0

I have a query that make some joins.

SELECT feedback.note as notes, 
to_char(feedback.data_compilazione,'DD/MM/YYYY') as date, 
CAST(feedback.punteggio AS INT) as score, 
upper(substring(cliente.nome from '^.') || '.' || 
substring(cliente.cognome from '^.') || '.') as customer, 
testo.testo as nation FROM feedback 
JOIN prenotazione ON prenotazione.id = feedback.id_prenotazione 
JOIN cliente ON cliente.id = prenotazione.id_cliente 
JOIN struttura ON struttura.id = prenotazione.id_struttura 
JOIN lingua ON cliente.id_lingua = lingua.id 
JOIN nazione ON cliente.codice_nazione = nazione.codice_iso 
JOIN testo ON testo.nome_tabella = 'nazione' AND testo.id_record = nazione.id AND
testo.id_lingua = lingua.id AND testo.id_tipo_testo = 1 WHERE struttura.id = 43 AND
lingua.sigla = E'en' AND feedback.punteggio >= 3 AND feedback.note <> '' 
ORDER BY feedback.data_compilazione DESC LIMIT 5

My problem is that I haven’t any explicit index onto my tables.

This means that this query took a long,long,long….long time to be executed.

AFAIK postresql creates an “implicit” index every time a declare a primary key, so I haven’t to add it “explicitly”.

This is the EXPLAIN of the query

"Limit  (cost=212.72..212.73 rows=1 width=208)"
"  ->  Sort  (cost=212.72..212.73 rows=1 width=208)"
"        Sort Key: feedback.data_compilazione"
"        ->  Nested Loop  (cost=1.11..212.71 rows=1 width=208)"
"              ->  Nested Loop  (cost=1.11..206.86 rows=1 width=212)"
"                    Join Filter: (("outer".codice_nazione)::text = ("inner".codice_iso)::text)"
"                    ->  Nested Loop  (cost=1.11..201.63 rows=1 width=223)"
"                          ->  Nested Loop  (cost=1.11..195.60 rows=1 width=187)"
"                                Join Filter: ("outer".id = "inner".id_cliente)"
"                                ->  Nested Loop  (cost=1.11..45.18 rows=1 width=183)"
"                                      Join Filter: ("outer".id_lingua =    "inner".id_lingua)"
"                                      ->  Index Scan using testo_pkey on testo  (cost=0.00..6.27 rows=1 width=40)"
"                                            Index Cond: (((nome_tabella)::text = 'nazione'::text) AND (id_tipo_testo = 1))"
"                                      ->  Hash Join  (cost=1.11..38.86 rows=4 width=155)"
"                                            Hash Cond: ("outer".id_lingua = "inner".id)"
"                                            ->  Seq Scan on cliente  (cost=0.00..33.47 rows=847 width=151)"
"                                            ->  Hash  (cost=1.11..1.11 rows=1 width=4)"
"                                                  ->  Seq Scan on lingua  (cost=0.00..1.11 rows=1 width=4)"
"                                                        Filter: ((sigla)::text = 'en'::text)"
"                                ->  Seq Scan on prenotazione  (cost=0.00..150.05 rows=30 width=12)"
"                                      Filter: (43 = id_struttura)"
"                          ->  Index Scan using feedback_id_prenotazione_key on feedback  (cost=0.00..6.01 rows=1 width=44)"
"                                Index Cond: ("outer".id = feedback.id_prenotazione)"
"                                Filter: ((punteggio >= 3::double precision) AND (note <> ''::text))"
"                    ->  Index Scan using nazione_pkey on nazione  (cost=0.00..5.21 rows=1 width=11)"
"                          Index Cond: ("outer".id_record = nazione.id)"
"              ->  Index Scan using struttura_pkey on struttura  (cost=0.00..5.82 rows=1 width=4)"
"                    Index Cond: (id = 43)"

So I stopped to think about indexing on DB.
What is the best practise for creating index?
The solution is: create an index for every joined field?

And into my DB, what you suggest to index?

I’ve done some try (substantially index every field that is joined) and the query run now faster but not fast (about six second to retrive zero rows).
I suppose that mine solution isn’t the best.

Anybody can point my look at the right direction?

EDIT

If I add just an index (on prenotazione.id_cliente) all works in very few seconds (1,5 about). So, why add all indexes on FOREIGN keys make my query run slower?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:07:43+00:00Added an answer on May 31, 2026 at 9:07 am

    As a first rule of thumb, without even looking at the query plan, I would certainly put indexes on foreign keys that have a high (>100 distinct values) selectivity.

    Some databases put them without asking, Postgres doesn’t.
    We are talking about indexes on FOREIGN keys, not PRIMARY ones (these are obviously always provided).

    For instance, prenotazione.id_struttura, and feedback.id_prenotazione are likely candidates.

    On the other hand, a weekday column of 7 distinct values would not benefit from an index, unless there are thousands of mondays and very few sundays, in which case the index is selective for some values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,
I have a query that has 7 inner joins (because a lot of the
I have a L2S query with several joins that should returns 11 records in
I'm trying to make a select statement that joins results from three in-line table
I am using in C# MYsql .I have query that works if I run
I have a query that is dynamically built after looking up a field list
I have a query that looks like this: public IList<Post> FetchLatestOrders(int pageIndex, int recordCount)
I have a query that I'm executing from a .NET application to a SQL
I have a query that is currently using a correlated subquery to return the
I have a query that works on Postgresql 7.4 but not on Postgresql 8.3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.