SQL method for calculating timestamp difference

SQL method for calculating timestamp difference

SQL method for calculating timestamp difference

Overview

Sometimes we need to find certain records by time, for example: calculate the records one hour before the sales time.
Usually we can use MYSQL's timestampdiff function to do this, but this cannot use the index, and if the amount of data is large, it will cause slow queries.

Use code to calculate the time and then pass it to SQL

We can use JAVA code to calculate the time first, and then pass it to the SQL statement to avoid using MYSQL functions.

public long xxxx(long sellTimeFrom){
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date(sellTimeFrom));
    calendar.set(Calendar.HOUR_OF_DAY,calendar.get(Calendar.HOUR_OF_DAY) - 1);
    return calendar.getTime().getTime();
}

This will calculate the time one hour before the sale time. Then pass it into the SQL statement where you write the code snippet. In this way, if the sales time field has an index, the index can be used.

Thank you for reading, I hope it can help you, thank you for your support of this site!

You may also be interested in:
  • MySQL slow query pitfalls
  • Solving the abnormality of mysql datetime query
  • Why MySQL can ignore time zone issues when using timestamp?
  • Pitfalls and solutions encountered in MySQL timestamp comparison query

<<:  Problems and solutions of using TweenMax animation library in angular

>>:  js implements a simple method of encapsulating jQuery and a detailed explanation of chain operations

Recommend

10 tips for designing useful, easy-to-use web applications

Here are 10 tips on how to design better-usable w...

Detailed explanation of JDBC database link and related method encapsulation

Detailed explanation of JDBC database link and re...

WeChat applet realizes the nine-square grid effect

This article shares the specific code for the WeC...

MySQL 5.7.21 winx64 installation and configuration method graphic tutorial

This article summarizes the notes for installing ...

How to solve the front-end cross-domain problem using Nginx proxy

Preface Nginx (pronounced "engine X") i...

htm beginner notes (must read for beginners)

1. What is HTML HTML (HyperText Markup Language):...

Reasons why MySQL cancelled Query Cache

MySQL previously had a query cache, Query Cache. ...

Graphic tutorial on installing Mac system in virtual machine under win10

1. Download the virtual machine version 15.5.1 I ...

Sharing tips on using vue element and nuxt

1. Element time selection submission format conve...

A brief discussion on MySQL temporary tables and derived tables

About derived tables When the main query contains...

MySQL log settings and viewing methods

MySQL has the following logs: Error log: -log-err...

CSS screen size adaptive implementation example

To achieve CSS screen size adaptation, we must fi...

Handwriting implementation of new in JS

Table of contents 1 Introduction to the new opera...

Table setting background image cannot be 100% displayed solution

The following situations were discovered during d...