λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

λͺ©λ‘μ΄ μ—†μŠ΅λ‹ˆλ‹€.

[MySQL] SYSDATE() 와 NOW()의 차이

πŸ›’ DB/MySQL
    λ°˜μ‘ν˜•

    MySQLMySQL



    μ˜€λŠ˜μ€ MySQL의 λ‚΄λΆ€μ μœΌλ‘œ λ‚΄μž₯λ˜μ–΄ μžˆλŠ” ν•¨μˆ˜(Built-inν•¨μˆ˜)λ₯Ό 정리해보렀고 ν•΄μš”


    MySQL의 Built-in ν•¨μˆ˜ μ€‘μ—λŠ” ν˜„μž¬ λ‚ μ§œ 및 μ‹œκ°„ 정보λ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜κ°€ SYSDATE()와 NOW() μ΄λ ‡κ²Œ 2κ°œκ°€ μžˆμ–΄μš”


    SYSDATE()와 NOW()의 μž‘λ™ 방식은 쿼리의 μ‹€ν–‰ κ³„νšμ— μƒλ‹Ήν•œ 영ν–₯을 λ―ΈμΉ  μ •λ„λ‘œ 영ν–₯λ ₯이 μ»€μš”


    1. MySQL 메뉴얼


    SYSDATE() returns the time at which it executes.
    This differs from the behavior for NOW(), which returns a constant time that indicates the time at which the statement began to execute.
    (Within a stored function or trigger, NOW() returns the time at which the function or triggering statement began to execute.)
     
    mysql> SELECT NOW(), SLEEP(2), NOW();
    +---------------------+----------+---------------------+
    | NOW()               | SLEEP(2| NOW()               |
    +---------------------+----------+---------------------+
    | 2017-11-28 14:52:34 |       0  | 2017-11-28 14:52:34 |
    +---------------------+----------+---------------------+
     
    mysql> SELECT SYSDATE(), SLEEP(2), SYSDATE();
    +---------------------+----------+---------------------+
    | SYSDATE()           | SLEEP(2| SYSDATE()           |
    +---------------------+----------+---------------------+
    | 2017-11-28 14:52:43 |       0  | 2017-11-28 14:52:45 |
    +---------------------+----------+---------------------+
     
    cs


    메뉴얼에도 μžμ„Ένžˆ λ‚˜μ™€ μžˆλ“―μ΄ SYSDATE() ν•¨μˆ˜λŠ” νŠΈλžœμ μ…˜μ΄λ‚˜ 쿼리 λ‹¨μœ„μ™€λŠ” μ „ν˜€ 상관없이 κ·Έ ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜λŠ” μ‹œμ μ˜ μ‹œκ°μ„ λ°˜ν™˜ν•΄μ€˜μš”


    λ°˜λ©΄μ— NOW()λŠ” ν•˜λ‚˜μ˜ 쿼리 λ‹¨μœ„λ‘œ λ™μΌν•œ μ‹œκ°μ„ λ°˜ν™˜ν•΄μš” 즉, μ•„λž˜μ™€ 같은 쿼리가 μžˆλ‹€κ³  ν–ˆμ„ λ•Œ


    SELECT fd1, fd2, fd3 FROM tab1 WHERE fd_dt > SYSDATE();
     
    cs


    λ§Œμ•½, 이 ν…Œμ΄λΈ”μ΄ λ ˆμ½”λ“œκ°€ λ„ˆλ¬΄ λ§Žμ•„μ„œ 처음 λ ˆμ½”λ“œλΆ€ν„° λκΉŒμ§€ μŠ€μΊ”ν•˜λŠ”λ° 1μ‹œκ°„μ΄ κ±Έλ¦°λ‹€λ©΄, 처음 λ ˆμ½”λ“œλ₯Ό 비ꡐ할 λ•Œμ—λŠ” SYSDATE()의 λ°˜ν™˜κ°’μ΄ '2017-11-28 00:00:00' μ΄μ—ˆλ‹€λ©΄


    λ§ˆμ§€λ§‰ λ ˆμ½”λ“œλ₯Ό 비ꡐ할 λ•Œμ—λŠ” SYSDATE()의 λ°˜ν™˜κ°’μ€ '2017-11-28 01:00:00' κ°€ λ˜μ–΄ 첫번째 λ ˆμ½”λ“œμ™€ λ§ˆμ§€λ§‰ λ ˆμ½”λ“œμ˜ 비ꡐ μ‹œμ μ΄ λ‹¬λΌμ Έμš”!!


    ν•œλ§ˆλ””λ‘œ NOW()의 λ°˜ν™˜κ°’μ€ μƒμˆ˜μ΄μ§€λ§Œ SYSDATE()의 λ°˜ν™˜κ°’μ€ μƒμˆ˜κ°’μ΄ μ•„λ‹Œκ±°μ—μš”


    μ΄λ ‡κ²Œλ˜λ©΄ 쿼리 μ‹€ν–‰ κ³„νšμ΄ λ°”λ€Œκ²Œ λ˜λŠ” κ²½μš°κ°€ μƒκΈ°λŠ”λ° λ‹€μŒκ³Ό 같은 κ²½μš°μ—μš”!




    2. 예제


    CREATE TABLE user (
      id mediumint(9NOT NULL AUTO_INCREMENT,
      name varchar(20DEFAULT NULL,
      age tinyint(3unsigned DEFAULT NULL,
      sex enum('MALE','FEMALE'DEFAULT NULL,
      regdt datetime NOT NULL,
      PRIMARY KEY (id),
      KEY ix_regdt (regdt)
    ENGINE=InnoDB;
    cs


    mysql> explain select * from user where regdt>now();
    +----+-------------+-------+-------+----------+---------+------+------+
    | id | select_type | table | type  | key      | key_len | ref  | rows |
    +----+-------------+-------+-------+----------+---------+------+------+
    |  1 | SIMPLE      | user  | range | ix_regdt | 8       | NULL |   1  |
    +----+-------------+-------+-------+----------+---------+------+------+
    1 row in set (0.00 sec)
     
    mysql> explain select * from user where regdt>sysdate();   
    +----+-------------+-------+------+------+---------+------+------+
    | id | select_type | table | type | key  | key_len | ref  | rows |
    +----+-------------+-------+------+------+---------+------+------+
    |  1 | SIMPLE      | user  | ALL  | NULL | NULL    | NULL | 4822 |
    +----+-------------+-------+------+------+---------+------+------+
    1 row in set (0.00 sec)
    cs



    SYSDATE()λŠ” λΉ„κ΅λ˜λŠ” μΉΌλŸΌμ— μΈλ±μŠ€κ°€ μ„€μ •λ˜μ–΄ μžˆλ“ μ§€ 없든지 상관없이 Full-Table-Scan을 ν•  수 밖에 μ—†λŠ” κ΅¬μ‘°μ—μš”


    이런 문제λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄μ„œ SYSDATE() μ‚¬μš© κΈˆμ§€ λ˜λŠ” --sysdate-is-now μ˜΅μ…˜μ„ μ„€μ •ν•˜μ—¬ MySQL Serverλ₯Ό λŒλ¦¬λŠ” 방법이 μžˆμ–΄μš”




    μ΄λ ‡κ²Œ MySQL의 SYSDATE()와 NOW()의 차이점을 μ •λ¦¬ν•΄λ΄€μ–΄μš”~


    좜처 : http://intomysql.blogspot.kr/2010/12/sysdate-now.html

    λ°˜μ‘ν˜•