"Mysql/hive/redis"의 두 판 사이의 차이

ph
이동: 둘러보기, 검색
54번째 줄: 54번째 줄:
 
===show tables like===
 
===show tables like===
 
  show tables in db_name like 'prefix_*'
 
  show tables in db_name like 'prefix_*'
 +
 +
===random sampling===
 +
<pre>select * from my_table
 +
where rand() <= 0.0001
 +
distribute by rand()
 +
sort by rand()
 +
limit 10000;</pre>
 +
or
 +
<pre>select * from my_table
 +
distribute by rand()
 +
sort by rand()
 +
limit 10000;<pre>
 +
[http://www.joefkelley.com/736/]
  
 
==Redis==
 
==Redis==

2018년 3월 9일 (금) 09:58 판

Rename table

ALTER TABLE table_name RENAME TO new_table_name;

[1]

Mysql

Create user

CREATE USER 'newuser'@'localhost' IDENTIFIED BY ‘password';
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@‘localhost';
FLUSH PRIVILEGES;
DROP USER ‘demo’@‘localhost’;

[2]

LEFT JOIN only first row

뾰족한 방법이 없는듯. 아래와 같이 그냥 한다[3]

SELECT *
FROM feeds f
LEFT JOIN artists a ON a.artist_id = (
    SELECT artist_id
    FROM feeds_artists fa 
    WHERE fa.feed_id = f.id
    LIMIT 1
)
WHERE f.id = ‘13815’

backup all

shell> mysqldump --all-databases > dump.sql

[4]

full join

없다. left join, right join을 union한다.

(SELECT ... FROM tbl1 LEFT JOIN tbl2 ...) UNION ALL (SELECT ... FROM tbl1 RIGHT JOIN tbl2 ... WHERE tbl1.col IS NULL)

이 질문의 두번째 댓글.

중복 제거

SELECT name, COUNT(*) c FROM table GROUP BY name HAVING c > 1;

[5] 쉬운것도 어렵게 생각하는 내 자신이 놀라워서 남겨둠.

HIVE

Hive Tutorial

view partitions

show partitions table;

http://stackoverflow.com/q/15616290/766330

특정 partition만을 대상으로 조회하는 특별한 쿼리는 없다. 파티션 정보를 where에 넣으면 hive가 자동으로 알아서 하는 것으로 보임. ref. how to select data from hive with specific partition?

median

select percentile(cast(age as BIGINT), 0.5) from table_name

show tables like

show tables in db_name like 'prefix_*'

random sampling

select * from my_table
where rand() <= 0.0001
distribute by rand()
sort by rand()
limit 10000;

or

select * from my_table
distribute by rand()
sort by rand()
limit 10000;
[6]

Redis

pipe

* cluster모드에서는 pipe가 동작하지 않는다. 각 서버(slot)에 알맞게 알아서 날려주어야 함. ** 소스에서 key_to_slot함수 참고. ** slot 확인 : $ redis-cli cluster nodes * 명령을 그대로 텍스트파일에 넣되 windows식 행바꿈이 필요하다. 참고 ** 굳이 redis protocol 안써도 됨 * cluster모드에서 pattern으로 key찾아 지우기 코드: http://stackoverflow.com/a/43841705/766330 누가했는지 잘했구만