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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:28:28+00:00 2026-05-16T21:28:28+00:00

What’s a common/best practice for database design when it comes to improving performance on

  • 0

What’s a common/best practice for database design when it comes to improving performance on count(1) queries? (I’m currently using SQLite)

I’ve normalized my data, it exists on multiple tables, and for simple things I want to do on a single table with a good index — queries are acceptably quick for my purposes.

eg:

SELECT count(1) from actions where type='3' and area='5' and employee='2533';

But when I start getting into multiple table queries, things get too slow (> 1 second).

SELECT count(1) 
  from
  (SELECT SID from actions 
      where type='3' and employee='2533' 
   INTERSECT 
     SELECT SID from transactions where currency='USD') x;

How should I cache my results? What is a good design?
My natural reaction is to add a table solely for storing rows of cached results per employee?

  • 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-16T21:28:29+00:00Added an answer on May 16, 2026 at 9:28 pm

    Edit

    Design patterns like Command Query Responsibility Segregation (CQRS) specifically aim to improve the read side performance of data access, often in distributed systems and at enterprise scale.

    • Commands are issued to indicate ‘transactions’ or ‘change / updates’ to data
    • When a system processes these commands (e.g. by updating database tables), the new state of the affected objects is ‘broadcast’
    • Systems which are interested (such as a user interface or a queryable REST API) will then subscribe to these data changes, and then ‘shape’ the updated data to their specific needs
    • This updated data is then cached (often called a ‘Read Store’)

    Another pattern commonly associated with CQRS is “Event Sourcing”, which stores, and then allows ‘replay’ of Commands, for various use cases.

    The above may be overkill for your scenario, but a very simple implementation of caching at an internal app level, could be via a Sqllite Trigger

    Assuming that there are many more ‘reads’ than writes to your actions or transactions tables,

    • You could create a cache tables specifically for “SID for actions by type by employee’ and one for “SID for transactions by Currency”, or even combine the two (depends on what other scenarios you have for querying)
    • You would then need to update these cache table(s) every time the underlying action or transactions tables update. One cheap (and nasty) way would be to provide an INSERT, UPDATE and DELETE trigger on the action and transactions table, which would then update the appropriate cache table(s).
    • Your ‘query’ interface would now primarily interact with the cache tables, using the ‘derived’ data (such as the counts).
    • You may still however need to handle cache miss scenarios, such as the initial ‘seed’ of these cache tables, or if the cache tables need to be regenerated.

    In addition to a local relational database like SqlLite, NoSql databases like MongoDb, Cassandra and Redis are frequently used as alternatives to read side caching in read-heavy environments (depending on the type and format of data that you need to cache). You would however need to handle alternative to synchronize data from your ‘master’ (e.g. SQLLite) database to these cache read stores – triggers obviously won’t cut it here.

    Original Answer

    If you are 100% sure that you are always repeating exactly the same query for the same customer, sure, persist the result.

    However, in most other instances, RDBMS usually handles caching just fine.

    The INTERSECT with the query

    SELECT SID from transactions where currency='USD'
    

    Could be problematic if there are a large number of transaction records with USD.

    Possibly you could replace this with a join?

    SELECT count(1) from 
    (
        SELECT t.[SID] 
        from
            transactions as t
            inner join
            (
                SELECT SID from actions where type='3' and employee='2533'
            ) as a
            on t.SID = a.SID
        where t.currency= 'USD'
    ) as a
    

    You might just check your indexes however:

    For

    • SELECT count(1) from actions where
      type=’3′ and area=’5′ and
      employee=’2533′
    • SELECT SID from actions where
      type=’3′ and employee=’2533′

    An index on Actions(Employee, Type) or Actions(Employee, Type, Area) would make sense (assuming Employee has highest selectivity, and depending on the selectivity of Type and Area).

    You can also compare this to an index on Actions(Employee, Type, Area, SID) as a covering index for your second query.

    And for the join above, you need an index on Transactions(SID, Currency)

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

Sidebar

Ask A Question

Stats

  • Questions 535k
  • Answers 535k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer remove the php from dataType, and read this read this… May 17, 2026 at 1:00 am
  • Editorial Team
    Editorial Team added an answer Here is the link of a book on this topic:… May 17, 2026 at 1:00 am
  • Editorial Team
    Editorial Team added an answer ORDER BY IFNULL(PublishOn, CreatedOn), CreatedOn May 17, 2026 at 1:00 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a JSP page retrieving data and when single or double quotes are
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and

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.