How to count how many records got inserted within 5 minutes from now in a single query?

Michael Kruglos

This is mostly a theoretical question, and it's mostly about MySQL. Can I write a single query that will give me a number of records inserted from the time the query started to run until it ended, assuming the table has not timestamps etc, so this info cannot be inferred from the data in the table.

I tired this (and maybe it'll clarify the above):

select -(count(*) - (sleep(300) + count(*)) from my_table;

But it doesn't seem to do the job. I know I can write a stored procedure to do it, but I'm just curious if there's a way to do it in a single query, without writing a new function/stored procedure.

Gordon Linoff

No, you really cannot, at least in theory. Databases support the ACID properties of transactions. The "I" in ACID stands for isolation, which specifically means that two queries do not interfere with each other. In other words, a query should not see inserts that happen after the query begins.

In practice, depending on settings, SELECT does not necessarily behave as its own transaction. However, it only sees the database as it is at any given instant, rather than knowing when particular changes occur.

There are proper ways to accomplish what you want. One simple method is to completely lock the table for the SELECT (in MySQL you can can do that with the for update directive). The query still will not be able to count the number of new rows. But, it will know the answer anyway: 0.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to find dates that are within 5 minutes of now()

How to delete records of orders that is canceled within 5 minutes in database?

Laravel how many rows inserted in an insert query?

Got DatetimeIndex. Now how do I get these records to excel?

How to get records with single query

How to get count or records returned from json_agg() query?

How to get the sum of a count of records (from a previous query)

How to count records from an specific query in Oracle SQL

How to count records with hour within range

Count how many records are in a CSV Python?

Loop to Count how many records in the table

How to query the last 5 records?

Laravel 4.2 count how many rows are inserted today

SQL Count how many outliers within a group by

Oracle 11g count how many Mondays and Thursdays from a starting date until now

CloudWatch Insights Query - How to get a single count from counts

How to combine values (count) from different queries into a single query

How to count rows from multiple tables in one single sql query

Select records count from multiple tables in a single query

How to write a query for one to many mapped records

In a single SQL query how many indexes can the query use from a single table?

How to squash records in a table using a single query?

PostgreSQL how to count records in a recursive query

How many elements can be inserted in an array of size 5?

How to calculate days, hours, minutes from now to certain date in swift

How to display latest inserted records?

How to query for a count within a count (without using a sub query)?

How to copy newly inserted records from local database to different database

How can a query with a SQL scalar function go from running in 5 minutes to 5 hours