人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

Mysql數(shù)據(jù)庫(kù)中datetime、bigint、timestamp來(lái)表示時(shí)間選擇,誰(shuí)來(lái)存儲(chǔ)時(shí)間效率最高

發(fā)布日期:2022-02-08 10:40 | 文章來(lái)源:gibhub

數(shù)據(jù)庫(kù)中可以用datetime、bigint、timestamp來(lái)表示時(shí)間,那么選擇什么類型來(lái)存儲(chǔ)時(shí)間比較合適呢?

# 后數(shù)據(jù)準(zhǔn)備

通過(guò)程序往數(shù)據(jù)庫(kù)插入50w數(shù)據(jù)

數(shù)據(jù)表:

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `time_date` datetime NOT NULL,
  `time_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `time_long` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `time_long` (`time_long`),
  KEY `time_timestamp` (`time_timestamp`),
  KEY `time_date` (`time_date`)
) ENGINE=InnoDB AUTO_INCREMENT=500003 DEFAULT CHARSET=latin1

其中time_long、time_timestamp、time_date為同一時(shí)間的不同存儲(chǔ)格式

實(shí)體類users

/**
 * @author hetiantian
 * @date 2018/10/21
 * */
@Builder
@Data
public class Users {
    /**
     * 自增唯一id
     * */
    private Long id;
    /**
     * date類型的時(shí)間
     * */
    private Date timeDate;
    /**
     * timestamp類型的時(shí)間
     * */
    private Timestamp timeTimestamp;
    /**
     * long類型的時(shí)間
     * */
    private long timeLong;
}

dao層接口

/**
 * @author hetiantian
 * @date 2018/10/21
 * */
@Mapper
public interface UsersMapper {
    @Insert("insert into users(time_date, time_timestamp, time_long) value(#{timeDate}, #{timeTimestamp}, #{timeLong})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    int saveUsers(Users users);
}

測(cè)試類往數(shù)據(jù)庫(kù)插入數(shù)據(jù)

public class UsersMapperTest extends BaseTest {
    @Resource
    private UsersMapper usersMapper;
    @Test
    public void test() {
        for (int i = 0; i < 500000; i++) {
            long time = System.currentTimeMillis();
            usersMapper.saveUsers(Users.builder().timeDate(new Date(time)).timeLong(time).timeTimestamp(new Timestamp(time)).build());
        }
    }
}

生成數(shù)據(jù)代碼方至github:https://github.com/TiantianUpup/sql-test/ 如果不想用代碼生成,而是想通過(guò)sql文件倒入數(shù)據(jù),文末附sql文件網(wǎng)盤(pán)地址。

# sql查詢速率測(cè)試

通過(guò)datetime類型查詢:

select count(*) from users where time_date >="2018-10-21 23:32:44" and time_date <="2018-10-21 23:41:22"

耗時(shí):0.171

通過(guò)timestamp類型查詢

select count(*) from users where time_timestamp >= "2018-10-21 23:32:44" and time_timestamp <="2018-10-21 23:41:22"

耗時(shí):0.351

通過(guò)bigint類型查詢

select count(*) from users where time_long >=1540135964091 and time_long <=1540136482372

耗時(shí):0.130s

結(jié)論 在InnoDB存儲(chǔ)引擎下,通過(guò)時(shí)間范圍查找,性能bigint > datetime > timestamp

# sql分組速率測(cè)試


使用bigint 進(jìn)行分組會(huì)每條數(shù)據(jù)進(jìn)行一個(gè)分組,如果將bigint做一個(gè)轉(zhuǎn)化在去分組就沒(méi)有比較的意義了,轉(zhuǎn)化也是需要時(shí)間的

通過(guò)datetime類型分組:

select time_date, count(*) from users group by time_date

耗時(shí):0.176s

通過(guò)timestamp類型分組:

select time_timestamp, count(*) from users group by time_timestamp

耗時(shí):0.173s

結(jié)論 在InnoDB存儲(chǔ)引擎下,通過(guò)時(shí)間分組,性能timestamp > datetime,但是相差不大

# sql排序速率測(cè)試

通過(guò)datetime類型排序:

select * from users order by time_date

耗時(shí):1.038s

通過(guò)timestamp類型排序

select * from users order by time_timestamp

耗時(shí):0.933s

通過(guò)bigint類型排序

select * from users order by time_long

耗時(shí):0.775s

結(jié)論:在InnoDB存儲(chǔ)引擎下,通過(guò)時(shí)間排序,性能bigint > timestamp > datetime

# 小結(jié)

如果需要對(duì)時(shí)間字段進(jìn)行操作(如通過(guò)時(shí)間范圍查找或者排序等),推薦使用bigint,如果時(shí)間字段不需要進(jìn)行任何操作,推薦使用timestamp,使用4個(gè)字節(jié)保存比較節(jié)省空間,但是只能記錄到2038年記錄的時(shí)間有限。

文中sql文件網(wǎng)盤(pán)地址: 鏈接: https://pan.baidu.com/s/1cCRCxtTlPriXMERGsbnb_A 提取碼: hbq2

到此這篇關(guān)于Mysql數(shù)據(jù)庫(kù)中datetime、bigint、timestamp來(lái)表示時(shí)間選擇,誰(shuí)來(lái)存儲(chǔ)時(shí)間效率最高的文章就介紹到這了,更多相關(guān)數(shù)據(jù)庫(kù)datetime、bigint、timestamp內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

國(guó)外服務(wù)器租用

版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開(kāi)通

自選配置、實(shí)時(shí)開(kāi)通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問(wèn)服務(wù)

1對(duì)1客戶咨詢顧問(wèn)

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部