MySQL
μ€λμ 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(9) NOT NULL AUTO_INCREMENT, name varchar(20) DEFAULT NULL, age tinyint(3) unsigned 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