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 7601955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:17:25+00:00 2026-05-30T23:17:25+00:00

I have two tables: poll_response (poll_id, option_id, user_id) (about 500,000 rows, with 500 unique

  • 0

I have two tables:

poll_response (poll_id, option_id, user_id)
(about 500,000 rows, with 500 unique polls, 1000 unique options, and 25000 unique users)

preferred_users (user_id)
(about 800 rows)

I’d like to determine what percentage of the users who select each option are “preferred users” (i.e. those with high reputation). Other users can respond to the poll; in order to identify that the response came from a preferred user, the join is required to the preferred_users table.

Here’s what I’ve got:

SELECT option_id, count(*) AS all_votes, count(preferred_users.user_id) AS preferred_votes
FROM response
LEFT JOIN preferred_users ON response.user_id = preferred_users.user_id
GROUP BY option_id

The query spits out a table like this:

| option_id | all_votes | preferred_votes
| 1         | 500       | 150
| 2         | 550       | 250
| 3         | 525       | 300

I can then do the math to determine the percentage.

The problem is that the query often times out — meaning it takes well over a minute to complete.

Is there any way to get rid of the left join or otherwise optimize the query?

  • 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-30T23:17:27+00:00Added an answer on May 30, 2026 at 11:17 pm

    have you tried splitting it into two queries – one for the total and one for the preferred users? i suspect that what is causing it to run slowly is running through the entries in group counting the non-null entries (but you could see yourself by using explain).

    in other words:

    select option_id, count(*) from response group by option_id
    select option_id, count(*) from response, preferred_users where response.user_id = preferred_user.id group by option_id
    

    you could even join them:

    select * from 
      (select option_id, count(*) as total from response group by option_id
       left join
       select option_id, count(*) as preferred from response, preferred_users where response.user_id = preferred_user.id group by option_id
       using (option_id))
    

    (not sure if i have the syntax right there, but you get the idea).

    also, you have an index on the preferred_users.id column too, right? and a foreign key relation from one to the other? if not, try with that first.

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

Sidebar

Related Questions

I have two tables users(id, username, password) and user_badges(user_id, badge_id, badge_slot) I would like
I have two tables inside a database. One stores unique userNames and a unique
I have two tables. One table contains information about Assets, another Table about their
I have two tables. Here is a simplified breakdown: Table #1 - Album: Rows:
I have two tables and want to compare rows on sqlite like this table1
I have two tables with a foreign key constraint how can I delete rows
i have two tables item_tb | item_id | user_id | --------------------- user_tb | user_id
I have two tables: Users and Profiles. A user may have a profile and
I have two tables USER and TRANSACTION i want to choose the data from
I have two tables: _____________________________ | wp_post | wp_postmeta | |______________|_____________| | 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.