濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > Mysql數(shù)據(jù)庫中datetime、bigint、timestamp來表示時(shí)間選擇,誰來存儲(chǔ)時(shí)間效率最高

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

熱門標(biāo)簽:大眾點(diǎn)評星級酒店地圖標(biāo)注 外東北地圖標(biāo)注 話務(wù)外呼系統(tǒng)怎么樣 臨清電話機(jī)器人 400電話可以辦理嗎 智能外呼系統(tǒng)復(fù)位 拉卡拉外呼系統(tǒng) 云南電商智能外呼系統(tǒng)價(jià)格 高清地圖標(biāo)注道路

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

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

通過程序往數(shù)據(jù)庫插入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 timeTimest

    /**
     * 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);
}

測試類往數(shù)據(jù)庫插入數(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/ 如果不想用代碼生成,而是想通過sql文件倒入數(shù)據(jù),文末附sql文件網(wǎng)盤地址。

# sql查詢速率測試

通過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

通過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

通過bigint類型查詢

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

耗時(shí):0.130s

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

# sql分組速率測試


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

通過datetime類型分組:

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

耗時(shí):0.176s

通過timestamp類型分組:

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

耗時(shí):0.173s

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

# sql排序速率測試

通過datetime類型排序:

select * from users order by time_date

耗時(shí):1.038s

通過timestamp類型排序

select * from users order by time_timestamp

耗時(shí):0.933s

通過bigint類型排序

select * from users order by time_long

耗時(shí):0.775s

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

# 小結(jié)

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

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

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

您可能感興趣的文章:
  • MySQL中datetime和timestamp的區(qū)別及使用詳解
  • Mysql中的Datetime和Timestamp比較
  • 淺談mysql導(dǎo)出表數(shù)據(jù)到excel關(guān)于datetime的格式問題
  • python3實(shí)現(xiàn)往mysql中插入datetime類型的數(shù)據(jù)
  • mysql datetime查詢異常問題解決
  • MySql用DATE_FORMAT截取DateTime字段的日期值
  • MySQL時(shí)間字段究竟使用INT還是DateTime的說明
  • MySQL 5.6 中TIMESTAMP with implicit DEFAULT value is deprecated錯(cuò)誤
  • mysql之TIMESTAMP(時(shí)間戳)用法詳解
  • MySQL錯(cuò)誤TIMESTAMP column with CURRENT_TIMESTAMP的解決方法
  • 解析mysql中UNIX_TIMESTAMP()函數(shù)與php中time()函數(shù)的區(qū)別
  • MySQL 中 datetime 和 timestamp 的區(qū)別與選擇

標(biāo)簽:溫州 山西 福州 三明 定西 揚(yáng)州 無錫 阿里

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Mysql數(shù)據(jù)庫中datetime、bigint、timestamp來表示時(shí)間選擇,誰來存儲(chǔ)時(shí)間效率最高》,本文關(guān)鍵詞  Mysql,數(shù)據(jù)庫,中,datetime,bigint,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Mysql數(shù)據(jù)庫中datetime、bigint、timestamp來表示時(shí)間選擇,誰來存儲(chǔ)時(shí)間效率最高》相關(guān)的同類信息!
  • 本頁收集關(guān)于Mysql數(shù)據(jù)庫中datetime、bigint、timestamp來表示時(shí)間選擇,誰來存儲(chǔ)時(shí)間效率最高的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    常宁市| 溧水县| 台南县| 昌邑市| 上饶县| 平度市| 柳河县| 新绛县| 西乡县| 无棣县| 报价| 惠东县| 黎川县| 华池县| 桐乡市| 岳阳市| 江川县| 阳城县| 修武县| 福海县| 西和县| 广灵县| 交口县| 肇州县| 北辰区| 循化| 皋兰县| 呼图壁县| 浪卡子县| 乌恰县| 华容县| 寿宁县| 抚顺市| 江安县| 扎兰屯市| 平顺县| 柘荣县| 普格县| 凯里市| 谢通门县| 鞍山市|