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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:09:57+00:00 2026-05-28T19:09:57+00:00

I have a table with unique usernames and a bunch of string data I

  • 0

I have a table with unique usernames and a bunch of string data I am keeping track of. Each user will have 1000 rows and when I select them I want to return them in the order they were added. Is the following code a necessary and correct way of doing this:

CREATE TABLE foo (
  username TEXT PRIMARY KEY,
  col1 TEXT,
  col2 TEXT,
  ...
  order_id INTEGER NOT NULL
);

CREATE INDEX foo_order_index ON foo(order_id);

SELECT * FROM foo where username = 'bar' ORDER BY order_id;
  • 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-28T19:09:59+00:00Added an answer on May 28, 2026 at 7:09 pm

    If each user will have 1000 rows, then username should not be the primary key. One option is to use the int identity column which all tables have (which optimizes I/O reads since it’s typically stored in that order).

    Read under “RowIds and the Integer Primary Key” @ http://www.sqlite.org/lang_createtable.html

    The data for each table in SQLite is stored as a B-Tree structure
    containing an entry for each table row, using the rowid value as the
    key. This means that retrieving or sorting records by rowid is fast.

    Because it’s stored in that order in the B-tree structure, it should be fast to order by the int primary key. Make sure it’s an alias for rowid though – more in that article.

    Also, if you’re going to be doing queries where username = ‘bob’, you should consider an index on the username column – especially there’s going to be many users which makes the index effective because of high selectivity. In contrast, adding an index on a column with values like 1 and 0 only leads to low selectivity and renders the index very ineffective. So, if you have 3 users 🙂 it’s not worth it.

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

Sidebar

Related Questions

We have a user table, every user has an unique email and username. We
I have a table full of items that each have a unique ItemID .
I have a user with his unique username in a mysql table, but I
I have a user table with userid and username columns, and both are unique.
I have table with a unique auto-incremental primary key. Over time, entries may be
Say I have a table with a unique positive integer field. I currently have
I have a table with 3 fields: ID (not unique, not primary key) timestamp
I have a table where every row has a unique id. The last table
I have a table called Table with columns: ID (int, primary key, clustered, unique
I have a database table that has a Unique Key constraint defined to avoid

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.