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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:45:40+00:00 2026-05-15T07:45:40+00:00

I am using SQLite and will port to MySQL (5) later. I wanted to

  • 0

I am using SQLite and will port to MySQL (5) later.

I wanted to know if I am doing something I shouldn’t be doing. I tried purposely to design so I’ll compare to 0 instead of 1 (I changed hasApproved to NotApproved to do this, not a big deal and I haven’t written any code). I was told I never need to write a subquery but I do here. My Votes table is just id, ip, postid (I don’t think I can write that subquery as a join instead?) and that’s pretty much all that is on my mind.

Naming conventions I don’t really care about since the tables are created via reflection and is all over the place.

select 
    id, 
    name, 
    body, 
    upvotes, 
    downvotes, 
    (select 1 from UpVotes where IPAddr=? AND post=Post.id) as myup, 
    (select 1 from DownVotes where IPAddr=@0 AND post=Post.id) as mydown
from Post 
where 
    flag = '0'
limit ?, ?"
  • 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-15T07:45:41+00:00Added an answer on May 15, 2026 at 7:45 am

    Since you’re asking about good practices… the “upvotes” and “downvotes” appearing in your Posts table looks like you’re duplicating data in your database. That’s a problem, because now you always have to worry whether or not the data is in sync and correct. If you want to know the number of upvotes then count them, don’t also store them in the Post table. I’m not positive that is what you’re doing, but it’s a guess.

    Onto your query… You will probably get better performance using a JOINed subquery instead of how you have it. With the scalar subqueries as columns they have to be run once for every row that is returned. That could be a pretty big performance hit if you’re returning a bunch of rows. Instead, try:

    SELECT
        P.id,
        P.name,
        P.body,
        P.upvotes,
        P.downvotes,
        COALESCE(UV.cnt, 0) AS upvotes2,
        COALESCE(DV.cnt, 0) AS downvotes2
    FROM
        dbo.Posts P
    LEFT OUTER JOIN (SELECT post_id, COUNT(*) cnt FROM dbo.UpVotes GROUP BY post_id) AS UV ON UV.post_id = P.id
    LEFT OUTER JOIN (SELECT post_id, COUNT(*) cnt FROM dbo.DownVotes GROUP BY post_id) AS DV ON DV.post_id = P.id
    

    Compare it to your own query and see if it gives you better performance.

    EDIT: A couple of other posters have advocated a single table for up/down votes. They are absolutely correct. That makes the query even easier and also probably much faster:

    SELECT
        P.id,
        P.name,
        P.body,
        P.upvotes,
        P.downvotes,
        SUM(CASE WHEN V.vote_type = 'UP' THEN 1 ELSE 0 END) AS upvotes2,
        SUM(CASE WHEN V.vote_type = 'DOWN' THEN 1 ELSE 0 END) AS downvotes2,
    FROM
        dbo.Posts P
    LEFT OUTER JOIN Votes V ON
        V.post_id = P.id
    GROUP BY
        P.id,
        P.name,
        P.body,
        P.upvotes,
        P.downvotes
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQLite database, eventually will be a MySQL database and I'm using
If I create an android database using SQLite it will be local, just for
So I'm writing an application in C# using SQLITE that will keep track of
When using SQLite database from Javascript you will use the following statement to get
where do the tables created using SQlite database will be stored in Android? how
Using SQLite I need to copy nearly all of an existing row from a
I am new to using SQLite databases and android mobile development. I am trying
I am using sqlite database for iphone app. but its crash on while loop
I was newbie in using sqlite database android. and now I have a problem
Right now I am using sqlite within a ios application, and I want to

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.