Skip to content

Instantly share code, notes, and snippets.

@rueian
rueian / pb.l
Created April 16, 2015 02:53
pb
%{
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include "y.tab.h"
void yyerror(char *);
%}
%%
@rueian
rueian / ssh-multi.sh
Last active March 18, 2016 12:31
Start multiple synchronized SSH connections with Tmux
#!/bin/bash
# ssh-multi : a script to ssh multiple servers over multiple tmux panes
# nomad-fr : https://gist.github.com/nomad-fr/8f2316c24c9d488a603b
# Based on D.Kovalov work : https://gist.github.com/dmytro/3984680
# config
#user=$USER # user use for ssh connection
user=root
@rueian
rueian / HashAggregate.1.sql
Last active December 21, 2019 17:13
HashAggregate.1.sql
postgres=#
EXPLAIN WITH subscribers AS MATERIALIZED (SELECT user_id FROM playlist_subscriptions WHERE list_id = 3343594)
SELECT * FROM devices WHERE user_id IN (SELECT user_id FROM subscribers);
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------
Nested Loop (cost=33227.59..58222.98 rows=294587 width=170)
CTE subscribers
-> Bitmap Heap Scan on playlist_subscriptions (cost=1350.13..32705.43 rows=23188 width=8)
Recheck Cond: (list_id = 3343594)
-> Bitmap Index Scan on playlist_subscriptions_list_id_user_id_uniq (cost=0.00..1344.34 rows=23188 width=0)
@rueian
rueian / HashAggregate.2.sql
Created December 21, 2019 17:40
HashAggregate.2.sql
postgres=#
EXPLAIN WITH subscribers AS MATERIALIZED (SELECT user_id FROM playlist_subscriptions WHERE list_id = 3343594)
SELECT * FROM devices WHERE user_id IN (SELECT user_id FROM subscribers);
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------
Hash Join (cost=33677.16..245826.56 rows=294587 width=170)
Hash Cond: (devices.user_id = subscribers.user_id)
CTE subscribers
-> Bitmap Heap Scan on playlist_subscriptions (cost=1350.13..32705.43 rows=23188 width=8)
Recheck Cond: (list_id = 3343594)
@rueian
rueian / HashAggregate.3.sql
Created December 21, 2019 17:41
HashAggregate.3.sql
postgres=# SET enable_hashjoin=off;
SET
postgres=# EXPLAIN WITH subscribers AS MATERIALIZED (SELECT user_id FROM playlist_subscriptions WHERE list_id = 3343594)
SELECT * FROM devices WHERE user_id IN (SELECT user_id FROM subscribers);
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------
Nested Loop (cost=33227.59..1384775.03 rows=294587 width=170)
CTE subscribers
-> Bitmap Heap Scan on playlist_subscriptions (cost=1350.13..32705.43 rows=23188 width=8)
Recheck Cond: (list_id = 3343594)
@rueian
rueian / HashAggregate.4.sql
Created December 21, 2019 18:01
HashAggregate.4.sql
postgres=#
EXPLAIN WITH subscribers AS (SELECT user_id FROM playlist_subscriptions WHERE list_id = 3343594)
SELECT * FROM devices WHERE user_id IN (SELECT user_id FROM subscribers);
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
Hash Join (cost=32995.28..241867.55 rows=294585 width=170)
Hash Cond: (devices.user_id = playlist_subscriptions.user_id)
-> Seq Scan on devices (cost=0.00..194390.62 rows=5516762 width=170)
-> Hash (cost=32705.43..32705.43 rows=23188 width=8)
-> Bitmap Heap Scan on playlist_subscriptions (cost=1350.13..32705.43 rows=23188 width=8)
@rueian
rueian / HashAggregate.5.sql
Created December 21, 2019 18:02
HashAggregate.5.sql
postgres=#
EXPLAIN SELECT * FROM devices WHERE user_id IN (SELECT user_id FROM playlist_subscriptions WHERE list_id = 3343594);
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
Hash Join (cost=32995.28..241867.55 rows=294585 width=170)
Hash Cond: (devices.user_id = playlist_subscriptions.user_id)
-> Seq Scan on devices (cost=0.00..194390.62 rows=5516762 width=170)
-> Hash (cost=32705.43..32705.43 rows=23188 width=8)
-> Bitmap Heap Scan on playlist_subscriptions (cost=1350.13..32705.43 rows=23188 width=8)
Recheck Cond: (list_id = 3343594)
@rueian
rueian / HashAggregate.6.sql
Created December 21, 2019 18:03
HashAggregate.6.sql
postgres=#
EXPLAIN SELECT devices.* FROM devices JOIN playlist_subscriptions ON devices.user_id = playlist_subscriptions.user_id WHERE list_id = 3343594;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
Hash Join (cost=32995.28..241867.55 rows=294585 width=170)
Hash Cond: (devices.user_id = playlist_subscriptions.user_id)
-> Seq Scan on devices (cost=0.00..194390.62 rows=5516762 width=170)
-> Hash (cost=32705.43..32705.43 rows=23188 width=8)
-> Bitmap Heap Scan on playlist_subscriptions (cost=1350.13..32705.43 rows=23188 width=8)
Recheck Cond: (list_id = 3343594)
@rueian
rueian / like1.sql
Created January 1, 2020 17:48
like1.sql
CREATE TABLE IF NOT EXISTS counters (
 id TEXT PRIMARY KEY COLLATE "C",
 value BIGINT
);
@rueian
rueian / like2.sql
Created January 1, 2020 17:50
like2.sql
postgres=# explain analyze select * from counters where id like 'ms:149895:%';
 QUERY PLAN
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 Index Scan using counters_pkey on counters (cost=0.56..8.58 rows=488 width=36) (actual time=0.014..0.022 rows=10 loops=1)
 Index Cond: ((id >= 'ms:149895:'::text) AND (id < 'ms:149895;'::text))
 Filter: (id ~~ 'ms:149895:%'::text)
 Planning Time: 0.148 ms
 Execution Time: 0.034 ms
(5 rows)