<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

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

          共 3030字,需瀏覽 7分鐘

           ·

          2021-08-26 08:55

          大家好,我是D哥

          點(diǎn)擊關(guān)注下方公眾號(hào),Java全套資料 都在這里

          作者:噠波甜
          來(lái)源:https://juejin.cn/post/6844903701094596615
          數(shù)據(jù)庫(kù)中可以用datetime、bigint、timestamp來(lái)表示時(shí)間,那么選擇什么類(lèi)型來(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í)體類(lèi)users
          /** * @author hetiantian * @date 2018/10/21 * */@Builder@Datapublic class Users {    /**     * 自增唯一id     * */    private Long id;
          /** * date類(lèi)型的時(shí)間 * */ private Date timeDate;
          /** * timestamp類(lèi)型的時(shí)間 * */ private Timestamp timeTimestamp;
          /** * long類(lèi)型的時(shí)間 * */ private long timeLong;}

          • dao層接口
          /** * @author hetiantian * @date 2018/10/21 * */@Mapperpublic 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è)試類(lèi)往數(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查詢(xún)速率測(cè)試


          • 通過(guò)datetime類(lèi)型查詢(xún):
          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類(lèi)型查詢(xún)
          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類(lèi)型查詢(xún)
          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類(lèi)型分組:
          select time_date, count(*) from users group by time_date

          耗時(shí):0.176s


          • 通過(guò)timestamp類(lèi)型分組:
          select time_timestamp, count(*) from users group by time_timestamp

          耗時(shí):0.173s


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


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


          • 通過(guò)datetime類(lèi)型排序:
          select * from users order by time_date

          耗時(shí):1.038s


          • 通過(guò)timestamp類(lèi)型排序
          select * from users order by time_timestamp

          耗時(shí):0.933s


          • 通過(guò)bigint類(lèi)型排序
          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í)間有限。

          --完--
          推薦閱讀:
          怎么接私貨?這個(gè)渠道你100%有用!請(qǐng)收藏!

          瀏覽 54
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  囯产精品久久 | 擦擦擦综合网 | 超碰99re | 日韩日屄视频在线免费观看 | 可以免费看的黄色网址 |