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

The Archive Base Latest Questions

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

I have the following table structure: +———-+————————+——+—–+——————————————+—————-+ | Field | Type | Null |

  • 0

I have the following table structure:


+----------+------------------------+------+-----+------------------------------------------+----------------+
| Field    | Type                   | Null | Key | Default                                  | Extra          |
+----------+------------------------+------+-----+------------------------------------------+----------------+
| id       | int(10) unsigned       | NO   | PRI | NULL                                     | auto_increment |
| body     | varchar(200)           | NO   |     | Hey now!                                 |                |
| flags    | int(10) unsigned       | NO   |     | 0                                        |                |
| views    | int(10) unsigned       | NO   |     | 1                                        |                |
+----------+------------------------+------+-----+------------------------------------------+----------------+

and I want to select “only rows that have not been flagged more than 5% of their total views, and have been viewed at least 5 times.”

Here is my query:


SELECT id,body
FROM tablename
WHERE id NOT IN (
    SELECT id
    FROM tablename
    WHERE flags/views * 100 > 5.0
    AND views > 5
    ORDER BY id DESC
)
ORDER BY id DESC
LIMIT 6

I’m thinking that selecting “every single row that has been flagged more than 30% of total views” is going to be a ton of overhead, especially when the table grows to a very large number of rows. Could someone please help me optimize this?

I was also thinking of creating a “flag_score” column and just updating that each time something is flagged, that way I could just select on the flag_score column instead of doing the math within the select (and save myself the extra select query). Does that sound like a good approach? Thanks a bunch.

Edit: The other problem I was having is that if I simply do something like:


SELECT *
FROM tabelname
WHERE flags/views * 100 > 5.0
AND views > 5
ORDER BY id DESC
LIMIT 5

… if 4 out of the 5 posts have been flagged, it will only return 1 row! And I’d like the statement to return 5 rows.

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

    The math is not very expensive, so as long as you have less than, say, 100 000 entries, you can do it like this with no worry.

    As suggested by yourself you can always cache the condition:

    UPDATE tablename
    SET is_over_30_percent = (flags/views * 100 > 5.0)
    WHERE id='id_of_updated_entry'
    

    which has the advantage that you can put an index on *is_over_30_percent* so the query takes no performance at all.

    Remember to put a combined index on *id_of_updated_entry* and date and id so the index can be used for selecting and ordering (ORDER BY is expensive).

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

Sidebar

Related Questions

I have the following table structure: CREATE TABLE [Report].[MesReport]( [MesReportID] [int] IDENTITY(1,1) NOT NULL,
I have the following table structure: <tr> <th><label for=ctl00_bodyContent_username>username:</label></th> <td class=field><input name=ctl00$bodyContent$username type=text id=ctl00_bodyContent_username
I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL
I have the following table structure: CREATE TABLE a ( a_id int(10) unsigned NOT
I have the following table structure Key int MemberID int VisitDate DateTime How can
I have following table structure. town: id (MEDINT,PRIMARY KEY,autoincrement), town(VARCHAR(150),not null), lat(FLOAT(10,6),notnull) lng(FLOAT(10,6),notnull) i
I have the following MySQL table structure: num field company phone website 1 Gas
I have the following table structure in my database: create table Encargado( ID int
I have a table structure like the following: table a: id int name varchar
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party

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.