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

  • Home
  • SEARCH
  • 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 7180275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:18:47+00:00 2026-05-28T17:18:47+00:00

I have a join table with the following structure: CREATE TABLE adjectives_friends ( adjective_id

  • 0

I have a join table with the following structure:

CREATE TABLE adjectives_friends
(
  adjective_id integer,
  friend_id integer
)
WITH (
  OIDS=FALSE
);
ALTER TABLE adjectives_friends
  OWNER TO rails;


CREATE UNIQUE INDEX index_adjectives_friends_on_adjective_id_and_friend_id
  ON adjectives_friends
  USING btree
  (adjective_id , friend_id );

CREATE UNIQUE INDEX index_adjectives_friends_on_friend_id_and_adjective_id
  ON adjectives_friends
  USING btree
  (friend_id , adjective_id );
ALTER TABLE adjectives_friends CLUSTER ON index_adjectives_friends_on_friend_id_and_adjective_id;

This table contains around ~50 million records.

The adjectives table is a look up table of ~150 entries. What I would like to do is find the friend that most closely matches a list of adjectives. Assume that the maximum number of adjectives a friend has is 10. So, I tried this query:

SELECT count(friend_id) count, friend_id
  FROM adjectives_friends
  where adjective_id in (1,2,3,4,5,6,7,8,9,10)
  group by friend_id
  order by count desc
  limit 100

This takes around ~10 seconds on my dev machine, with query plan

"Limit  (cost=831652.00..831652.25 rows=100 width=4)"
"  ->  Sort  (cost=831652.00..831888.59 rows=94634 width=4)"
"        Sort Key: (count(friend_id))"
"        ->  GroupAggregate  (cost=804185.31..828035.16 rows=94634 width=4)"
"              ->  Sort  (cost=804185.31..811819.81 rows=3053801 width=4)"
"                    Sort Key: friend_id"
"                    ->  Bitmap Heap Scan on adjectives_friends  (cost=85958.72..350003.24 rows=3053801 width=4)"
"                          Recheck Cond: (adjective_id = ANY ('{1,2,3,4,5,6,7,8,9,10}'::integer[]))"
"                          ->  Bitmap Index Scan on index_adjectives_friends_on_adjective_id_and_friend_id  (cost=0.00..85195.26 rows=3053801 width=0)"
"                                Index Cond: (adjective_id = ANY ('{1,2,3,4,5,6,7,8,9,10}'::integer[]))"

The order by is what is killing me, but I don’t know of a good way to avoid it. The count can’t be precomputed becasue the adjectives to be selected are completely arbitrary, and there are > 150 choose 10 combinations. Right now, I think that the best option is to grab the 100 best results on friend creation, save the results, then update it every n time intervals. This would be acceptable as the adjectives are expected to be switched that often, and I don’t the exact 100 best results. But, if I could get the query speed to around 1 – 2 seconds, that wouldn’t be neccessary. Any suggestions?

  • 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-28T17:18:49+00:00Added an answer on May 28, 2026 at 5:18 pm

    I don’t think you’ll do much better with that query plan. I’ll take your word that the count can’t be precomputed.

    I think your best bets are

    • Table tuning
    • Server tuning
    • Faster hardware

    If you can use smallint instead of integer, your tables and indexes will be narrower, more will fit into a page, and your queries should run faster. But smallint is a 2-byte integer, ranging from -32768 to +32767. If you need more id numbers than that, smallint won’t work.

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

Sidebar

Related Questions

I have a organization name table with the following structure given below: CREATE TABLE
I have the following structure SET SQL_MODE=NO_AUTO_VALUE_ON_ZERO; CREATE TABLE IF NOT EXISTS `sis_param_tax` (
I have another rather curious problem. I have the following structure: CREATE TABLE [dbo].[Event]
I have the following MySQL structure (minimized a lot): CREATE TABLE `site_movies` ( `id`
Working in a Rails App, I have the following table structure (pertinent columns only)
Let's say I have the following hypothetical data structure: create table country ( country_id
I have the following data structure and data: CREATE TABLE `parent` ( `id` int(11)
I have a basic recursive table setup in SQL 2008 with the following structure:
I have a base table of Participants and Answers, with the following structure: ParticipantId,
Assume I have a table that has the following structure: ================= | Id |

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.