Skip to content

Instantly share code, notes, and snippets.

@paulocheque
Last active February 27, 2016 16:52
Show Gist options
  • Select an option

  • Save paulocheque/2cf44c2694e4ebec4559 to your computer and use it in GitHub Desktop.

Select an option

Save paulocheque/2cf44c2694e4ebec4559 to your computer and use it in GitHub Desktop.

Revisions

  1. paulocheque revised this gist Dec 2, 2014. 2 changed files with 17 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions mysql.md
    Original file line number Diff line number Diff line change
    @@ -27,6 +27,16 @@ Insert or Update.

    INSERT INTO TABLE (col1) values (val1) ON DUPLICATE KEY UPDATE col1 = val1

    DELETE
    =====================

    DELETE gc.*
    FROM guide_category AS gc
    LEFT JOIN
    guide AS g
    ON g.id_guide = gc.id_guide
    WHERE g.title IS NULL


    Monitoring
    ======================
    7 changes: 7 additions & 0 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,13 @@ Select a random row
    ORDER BY RANDOM()
    LIMIT 1

    DELETE with join
    =====================

    DELETE FROM table as t using anothertable as x where t.id = x.refid and t.etc = etc;



    Counter
    ======================

  2. paulocheque revised this gist Dec 1, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions sql.md
    Original file line number Diff line number Diff line change
    @@ -29,6 +29,8 @@ UPDATE

    UPDATE table set column = column + 2

    UPDATE TABLE as main SET column=value
    FROM OTHERTABLE as x WHERE main.columnid = x.id and etc;

    Join
    ======
  3. paulocheque revised this gist Dec 1, 2014. 1 changed file with 16 additions and 1 deletion.
    17 changes: 16 additions & 1 deletion postgres.md
    Original file line number Diff line number Diff line change
    @@ -41,8 +41,19 @@ Dump/Restore
    Monitoring
    ===============

    http://www.postgresql.org/docs/9.3/static/monitoring-stats.html

    -- show active queries
    SELECT * FROM pg_stat_activity where state = 'active' order by query_start;
    SELECT * FROM pg_stat_activity order by query_start;

    -- show indexes information
    SELECT * FROM pg_stat_all_indexes;
    SELECT * FROM pg_stat_user_indexes;

    SELECT count(*) as cnt, usename FROM pg_stat_activity GROUP BY usename ORDER BY cnt DESC limit 5;

    -- DB
    SELECT version();
    SHOW server_version;
    SHOW server_version_num;
    @@ -52,13 +63,16 @@ Monitoring
    show ssl;
    SELECT * FROM pg_statio_user_tables;
    SELECT * FROM pg_stat_user_tables;
    SHOW rds.extensions;

    https://github.com/pganalyze/pganalyze-collector

    https://github.com/zalando/pg_view

    https://github.com/zalando/PGObserver

    https://pganalyze.com/docs/install/01_enabling_pg_stat_statements

    http://bucardo.org/wiki/Check_postgres

    brew install check_postgres
    @@ -67,4 +81,5 @@ http://bucardo.org/wiki/Check_postgres
    --action=backends
    --action=dbstats




  4. paulocheque revised this gist Dec 1, 2014. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion postgres.md
    Original file line number Diff line number Diff line change
    @@ -54,9 +54,17 @@ Monitoring
    SELECT * FROM pg_stat_user_tables;

    https://github.com/pganalyze/pganalyze-collector

    https://github.com/zalando/pg_view
    https://github.com/zalando/PGObserver

    https://github.com/zalando/PGObserver

    http://bucardo.org/wiki/Check_postgres

    brew install check_postgres
    check_postgres --host=host --port 5432 --dbname db --dbuser user --dbpass pass --action=connection
    --action=connection
    --action=backends
    --action=dbstats


  5. paulocheque revised this gist Dec 1, 2014. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -36,3 +36,27 @@ Dump/Restore

    pg_dump -v -h [hostname] -p 5432 -U [user] -Fc [database] > [flename]
    pg_restore -d [database] [filename]


    Monitoring
    ===============

    SELECT * FROM pg_stat_activity order by query_start;
    SELECT count(*) as cnt, usename FROM pg_stat_activity GROUP BY usename ORDER BY cnt DESC limit 5;
    SELECT version();
    SHOW server_version;
    SHOW server_version_num;
    SHOW all;
    SELECT pg_database.datname, pg_size_pretty(pg_database_size(pg_database.datname)) AS size FROM pg_database;
    select * from FROM pg_database;
    show ssl;
    SELECT * FROM pg_statio_user_tables;
    SELECT * FROM pg_stat_user_tables;

    https://github.com/pganalyze/pganalyze-collector
    https://github.com/zalando/pg_view
    https://github.com/zalando/PGObserver




  6. paulocheque revised this gist Nov 25, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions mysql.md
    Original file line number Diff line number Diff line change
    @@ -34,6 +34,7 @@ Monitoring
    show global status
    show global status like '%conn%';
    show global variables like 'max_connections';
    show global variables like '%conn%';
    show global status like 'max%';
    show variables like "%timeout%";

  7. paulocheque revised this gist Nov 25, 2014. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions mysql.md
    Original file line number Diff line number Diff line change
    @@ -31,9 +31,10 @@ Insert or Update.
    Monitoring
    ======================

    show global status
    show global status like '%conn%';
    show global variables like 'max_connections';
    show global status like 'max%';
    show variables like "%timeout%";
    show global status
    show global status like '%conn%';
    show global variables like 'max_connections';
    show global status like 'max%';
    show variables like "%timeout%";

    http://mysqlblog.fivefarmers.com/2013/08/01/practical-p_s-how-idle-are-your-connections/
  8. paulocheque revised this gist Nov 18, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions mysql.md
    Original file line number Diff line number Diff line change
    @@ -36,3 +36,4 @@ show global status like '%conn%';
    show global variables like 'max_connections';
    show global status like 'max%';
    show variables like "%timeout%";
    http://mysqlblog.fivefarmers.com/2013/08/01/practical-p_s-how-idle-are-your-connections/
  9. paulocheque revised this gist Nov 18, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions mysql.md
    Original file line number Diff line number Diff line change
    @@ -35,3 +35,4 @@ show global status
    show global status like '%conn%';
    show global variables like 'max_connections';
    show global status like 'max%';
    show variables like "%timeout%";
  10. paulocheque revised this gist Nov 18, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions mysql.md
    Original file line number Diff line number Diff line change
    @@ -31,5 +31,7 @@ Insert or Update.
    Monitoring
    ======================

    show global status
    show global status like '%conn%';
    show global variables like 'max_connections';
    show global status like 'max%';
  11. paulocheque revised this gist Nov 17, 2014. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions mysql.md
    Original file line number Diff line number Diff line change
    @@ -26,3 +26,10 @@ Insert or Update.
    ======================

    INSERT INTO TABLE (col1) values (val1) ON DUPLICATE KEY UPDATE col1 = val1


    Monitoring
    ======================

    show global variables like 'max_connections';
    show global status like 'max%';
  12. paulocheque revised this gist Nov 5, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions mysql.md
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,9 @@ Conditional Insert

    INSERT INTO TABLE(col1, col2) SELECT val1, val2 FROM dual where not exists (select * from TABLE where col1 = val1 and col2 = val2);


    INSERT INTO TABLE(col1, col2) SELECT val1 as col1, val2 as col2 FROM TABLE where col1 = val1 and col2 = val2 having count(*) = 0;

    -- To ignore unique contraint errors..
    INSERT IGNORE INTO TABLE....

  13. paulocheque revised this gist Nov 5, 2014. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions mysql.md
    Original file line number Diff line number Diff line change
    @@ -8,3 +8,18 @@ Select a random row
    SELECT column FROM table
    ORDER BY RAND()
    LIMIT 1


    Conditional Insert
    ======================

    INSERT INTO TABLE(col1, col2) SELECT val1, val2 FROM dual where not exists (select * from TABLE where col1 = val1 and col2 = val2);

    -- To ignore unique contraint errors..
    INSERT IGNORE INTO TABLE....


    Insert or Update.
    ======================

    INSERT INTO TABLE (col1) values (val1) ON DUPLICATE KEY UPDATE col1 = val1
  14. paulocheque revised this gist Oct 16, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,12 @@ Commands
    ======================

    postgres -D /usr/local/var/postgres

    sudo -u paulocheque createuser dev

    sudo -u postgres createuser dev

    createdb DATABASENAME

    Select a random row
    ======================
  15. paulocheque revised this gist Oct 15, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions sql.md
    Original file line number Diff line number Diff line change
    @@ -33,6 +33,8 @@ UPDATE
    Join
    ======

    http://stackoverflow.com/questions/406294/left-join-and-left-outer-join-in-sql-server

    WHERE Aluno.numAluno = Desempenho.numAluno;


  16. paulocheque revised this gist Oct 13, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -30,3 +30,6 @@ Dump/Restore
    pg_restore -h localhost db.bin.dump

    pg_restore --no-owner --clean --exit-on-error --disable-triggers --data-only --host localhost --port 5432 --username "paulocheque" --dbname "DBNAME" --no-password --verbose db.bin.dump

    pg_dump -v -h [hostname] -p 5432 -U [user] -Fc [database] > [flename]
    pg_restore -d [database] [filename]
  17. paulocheque revised this gist Oct 5, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion postgres.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,8 @@ Commands
    ======================

    postgres -D /usr/local/var/postgres
    psql -u postgres createuser -P application
    sudo -u paulocheque createuser dev
    sudo -u postgres createuser dev


    Select a random row
  18. paulocheque revised this gist Oct 4, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@ Commands
    ======================

    postgres -D /usr/local/var/postgres
    psql -u postgres createuser -P application


    Select a random row
    ======================
  19. paulocheque revised this gist Oct 4, 2014. 3 changed files with 9 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions mysql.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    Commands
    ======================
    sudo mysqld_safe

    Select a random row
    ======================

    5 changes: 5 additions & 0 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    Commands
    ======================

    postgres -D /usr/local/var/postgres

    Select a random row
    ======================

    File renamed without changes.
  20. paulocheque revised this gist Sep 22, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions general.md
    Original file line number Diff line number Diff line change
    @@ -21,6 +21,8 @@ WHERE
    WHERE nota BETWEEN 6.0 AND 10.0;

    ORDER BY curso DESC, nomeAluno DESC;

    LIMIT 100 OFFSET 10

    UPDATE
    ===========
  21. paulocheque revised this gist Sep 19, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,7 @@ Dump/Restore
    ======================

    pg_dump -h localhost -p 5432 DBNAME > db.sql
    pg_dump -h localhost -p 5432 DBNAME -U username -W password > db.sql
    psql -h localhost DBNAME < db.sql

    pg_dump -Fc -h localhost -p 5432 DBNAME > db.bin.dump
  22. paulocheque revised this gist Sep 16, 2014. 5 changed files with 36 additions and 26 deletions.
    3 changes: 1 addition & 2 deletions general.md
    Original file line number Diff line number Diff line change
    @@ -31,8 +31,7 @@ UPDATE
    Join
    ======

    WHERE Aluno.numAluno =
    Desempenho.numAluno;
    WHERE Aluno.numAluno = Desempenho.numAluno;



    14 changes: 5 additions & 9 deletions mysql.md
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,6 @@
    Select a random row with MySQL:
    Select a random row
    ======================

    SELECT column FROM table
    ORDER BY RAND()
    LIMIT 1
    Select a random row with PostgreSQL:

    SELECT column FROM table
    ORDER BY RANDOM()
    LIMIT 1
    SELECT column FROM table
    ORDER BY RAND()
    LIMIT 1
    11 changes: 6 additions & 5 deletions oracle.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    Select a random record with Oracle:
    Select a random record
    ======================

    SELECT column FROM
    ( SELECT column FROM table
    ORDER BY dbms_random.value )
    WHERE rownum = 1
    SELECT column FROM
    ( SELECT column FROM table
    ORDER BY dbms_random.value )
    WHERE rownum = 1
    27 changes: 20 additions & 7 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,23 @@
    select nextval('table')
    setval('table', value)
    Select a random row
    ======================

    pg_dump -h localhost -p 5432 DBNAME > db.sql
    psql -h localhost DBNAME < db.sql
    SELECT column FROM table
    ORDER BY RANDOM()
    LIMIT 1

    Counter
    ======================

    pg_dump -Fc -h localhost -p 5432 DBNAME > db.bin.dump
    pg_restore -h localhost db.bin.dump
    select nextval('table')
    setval('table', value)

    pg_restore --no-owner --clean --exit-on-error --disable-triggers --data-only --host localhost --port 5432 --username "paulocheque" --dbname "DBNAME" --no-password --verbose db.bin.dump
    Dump/Restore
    ======================

    pg_dump -h localhost -p 5432 DBNAME > db.sql
    psql -h localhost DBNAME < db.sql

    pg_dump -Fc -h localhost -p 5432 DBNAME > db.bin.dump
    pg_restore -h localhost db.bin.dump

    pg_restore --no-owner --clean --exit-on-error --disable-triggers --data-only --host localhost --port 5432 --username "paulocheque" --dbname "DBNAME" --no-password --verbose db.bin.dump
    7 changes: 4 additions & 3 deletions sqlserver.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    Select a random row with Microsoft SQL Server:
    Select a random row
    ======================

    SELECT TOP 1 column FROM table
    ORDER BY NEWID()
    SELECT TOP 1 column FROM table
    ORDER BY NEWID()
  23. paulocheque revised this gist Sep 16, 2014. 1 changed file with 31 additions and 21 deletions.
    52 changes: 31 additions & 21 deletions general.md
    Original file line number Diff line number Diff line change
    @@ -1,46 +1,56 @@
    Basic
    SELECT
    ==============

    SELECT DISTINCT curso FROM aluno;
    SELECT DISTINCT curso FROM aluno;

    SELECT curso, count(*)
    SELECT curso, count(*)
    FROM aluno
    GROUP BY curso
    HAVING count(*) > 1;

    UPDATE table SET col1=value1, col2=value2 WHERE col3=value3

    WHERE
    ======
    WHERE nomeAluno LIKE ?Jos?%?;

    WHERE numAluno IN ('22233', '77777777');
    WHERE nomeAluno LIKE ?Jos?%?;

    WHERE numAluno IN (SELECT numAluno FROM desempenho);
    WHERE numAluno IN ('22233', '77777777');

    WHERE nota BETWEEN 6.0 AND 10.0;
    WHERE numAluno IN (SELECT numAluno FROM desempenho);

    WHERE nota BETWEEN 6.0 AND 10.0;

    ORDER BY curso DESC, nomeAluno DESC;

    UPDATE
    ===========

    UPDATE table set column = column + 2

    ORDER BY curso DESC, nomeAluno DESC;

    Join
    ======

    WHERE Aluno.numAluno =
    Desempenho.numAluno;
    WHERE Aluno.numAluno =
    Desempenho.numAluno;



    more
    TRUNCATE
    ======
    TRUNCATE table
    TRUNCATE table CASCADE

    TRUNCATE table
    TRUNCATE table CASCADE

    DELETE
    ========

    DELETE FROM table1
    where table2Id IN (
    select id from table2
    where date >= '2014-08-01 00:00:00' and date <= '2014-09-15 23:59:59'
    );

    DELETE FROM table1
    where table2Id IN (
    select id from table2
    where date >= '2014-08-01 00:00:00' and date <= '2014-09-15 23:59:59'
    );

    delete from table1 a using table2 b
    where a.table2Id = b.id and b.date >= '2014-09-01 00:00:00' and b.date <= '2014-09-01 23:59:59';
    delete from table1 a using table2 b
    where a.table2Id = b.id and b.date >= '2014-09-01 00:00:00' and b.date <= '2014-09-01 23:59:59';
  24. paulocheque revised this gist Sep 15, 2014. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion general.md
    Original file line number Diff line number Diff line change
    @@ -33,4 +33,14 @@ Desempenho.numAluno;
    more
    ======
    TRUNCATE table
    TRUNCATE table CASCADE
    TRUNCATE table CASCADE


    DELETE FROM table1
    where table2Id IN (
    select id from table2
    where date >= '2014-08-01 00:00:00' and date <= '2014-09-15 23:59:59'
    );

    delete from table1 a using table2 b
    where a.table2Id = b.id and b.date >= '2014-09-01 00:00:00' and b.date <= '2014-09-01 23:59:59';
  25. paulocheque revised this gist Sep 12, 2014. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions general.md
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,15 @@
    Basic
    ==============

    SELECT DISTINCT curso FROM aluno;

    SELECT curso, count(*)
    FROM aluno
    GROUP BY curso
    HAVING count(*) > 1;

    UPDATE table SET col1=value1, col2=value2 WHERE col3=value3

    WHERE
    ======
    WHERE nomeAluno LIKE ?Jos?%?;
    @@ -23,6 +28,8 @@ Join
    WHERE Aluno.numAluno =
    Desempenho.numAluno;



    more
    ======
    TRUNCATE table
  26. paulocheque revised this gist Sep 11, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion general.md
    Original file line number Diff line number Diff line change
    @@ -23,4 +23,7 @@ Join
    WHERE Aluno.numAluno =
    Desempenho.numAluno;

    TRUNCATE table
    more
    ======
    TRUNCATE table
    TRUNCATE table CASCADE
  27. paulocheque revised this gist Sep 11, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion general.md
    Original file line number Diff line number Diff line change
    @@ -21,4 +21,6 @@ Join
    ======

    WHERE Aluno.numAluno =
    Desempenho.numAluno;
    Desempenho.numAluno;

    TRUNCATE table
  28. paulocheque revised this gist Sep 11, 2014. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions general.md
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,13 @@ SELECT curso, count(*)
    WHERE
    ======
    WHERE nomeAluno LIKE ?Jos?%?;

    WHERE numAluno IN ('22233', '77777777');

    WHERE numAluno IN (SELECT numAluno FROM desempenho);

    WHERE nota BETWEEN 6.0 AND 10.0;

    ORDER BY curso DESC, nomeAluno DESC;

    Join
  29. paulocheque revised this gist Aug 28, 2014. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion postgres.md
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,10 @@
    select nextval('table')
    setval('table', value)
    setval('table', value)

    pg_dump -h localhost -p 5432 DBNAME > db.sql
    psql -h localhost DBNAME < db.sql

    pg_dump -Fc -h localhost -p 5432 DBNAME > db.bin.dump
    pg_restore -h localhost db.bin.dump

    pg_restore --no-owner --clean --exit-on-error --disable-triggers --data-only --host localhost --port 5432 --username "paulocheque" --dbname "DBNAME" --no-password --verbose db.bin.dump
  30. paulocheque created this gist Aug 19, 2014.
    20 changes: 20 additions & 0 deletions general.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    SELECT DISTINCT curso FROM aluno;

    SELECT curso, count(*)
    FROM aluno
    GROUP BY curso
    HAVING count(*) > 1;

    WHERE
    ======
    WHERE nomeAluno LIKE ?Jos?%?;
    WHERE numAluno IN ('22233', '77777777');
    WHERE numAluno IN (SELECT numAluno FROM desempenho);
    WHERE nota BETWEEN 6.0 AND 10.0;
    ORDER BY curso DESC, nomeAluno DESC;

    Join
    ======

    WHERE Aluno.numAluno =
    Desempenho.numAluno;
    10 changes: 10 additions & 0 deletions mysql.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    Select a random row with MySQL:

    SELECT column FROM table
    ORDER BY RAND()
    LIMIT 1
    Select a random row with PostgreSQL:

    SELECT column FROM table
    ORDER BY RANDOM()
    LIMIT 1
    6 changes: 6 additions & 0 deletions oracle.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    Select a random record with Oracle:

    SELECT column FROM
    ( SELECT column FROM table
    ORDER BY dbms_random.value )
    WHERE rownum = 1
    2 changes: 2 additions & 0 deletions postgres.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    select nextval('table')
    setval('table', value)
    4 changes: 4 additions & 0 deletions sqlserver.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    Select a random row with Microsoft SQL Server:

    SELECT TOP 1 column FROM table
    ORDER BY NEWID()