INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_valueSELECT row1, row2
FROM table
ORDER BY row2 (ASC|DESC)DELETE FROM table_name
WHERE some_column=some_valueSELECT *
FROM Persons
WHERE City LIKE '%ville'CASE WHEN condition THEN result
[WHEN ...]
[ELSE result]
ENDSELECT DATE_FORMAT(`date`,'%Y-%m-%d') AS showdate
FROM tableFROM stockserialitems
WHERE expirationdate < utc_timestamp() + interval 90 daySELECT a, b, count(*) cnt
FROM table
GROUP BY a, b
HAVING cnt > 1
ORDER BY cnt asc;- 1992
- 1999
- 2003
- 2008
- new ones?
.tables
.schema tablename
SELECT name
FROM sqlite_master
WHERE type='table'sqlite3 my.db .dump
SELECT SUM("pgsize") FROM "dbstat" WHERE name='TABLENAME';
- NULL. The value is a NULL value.
- INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
- REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
- TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).
- BLOB. The value is a blob of data, stored exactly as it was input.
psql
$ psql -l
# in psql shell \l/var/lib/pgsql/data/postgresql.conf
SELECT * FROM pg_stat_activity ;Don't use SIGKILL, it will shut down the rest of PostgreSQL, and require a replay
pg_cancel_backend(pid int)
pg_terminate_backend(pid int)
| config | set |
|---|---|
shared_buffers |
About 1/4 of the memory in the system |
effective_cache_size |
should be ~ 2/3 of available RAM |
work_mem |
Depends on maxconnections |
maintenance_work_mem |
Used for vacuum, etc. "256 MB is reasonable" |
When you update a table in PostgreSQL, deleting or updating a row leaves behind old rows that were part of the transaction. To reclaim the space and prevent table bloat, you'll need to vacuum the database.
From 8.1 on, there's an autovacuum daemon which can be tuned. Have it aggressively vacuum high-frequency tables.
For really bad table bloat, use CLUSTER (not VACUUM FULL). It requires space for the in-use data while it runs, but is faster and prevents bloated indexes.
pg_dump -O -x -F t -b -f [filename.tar] [db name]pg_restore -F t -d [db name] [filename.tar]create databaseREINDEX { INDEX | TABLE | DATABASE | SYSTEM } name- http://www.postgresql.org/docs/9.2/static/functions-admin.html
- http://www.postgresql.org/docs/9.2/static/monitoring-stats.html
watch -n 1 mysqladmin --user=<user> --password=<password> processlistshow databases;show tables;-- Column names
describe TABLE_NAME;
-- Schema
show create table TABLE_NAME;
-- Indexes
show index from TABLE_NAME;mysqldump --all-databases --no-data
# --skip-add-drop-table
# --skip-comments-- contents
help contents
mysqladmin pingcreate database DATABASE_NAME;mysql -u username -p<password> database < filename.sqlmysqldump -u username -p<password> database > filename.sqldrop database DATABASE_NAME;-- Create a user
CREATE USER 'example_user'@'localhost' IDENTIFIED BY 'example_pass';
-- Revoke permissions for user
REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'example_user'@'localhost';
-- Delete a user
DROP USER 'example_user'@'localhost';-- show grants for current user
show grants;
-- show grants for particular user
show grants for 'user'@'example.com';
-- session variables
SHOW SESSION VARIABLES;
SET SESSION sort_buffer_size=1000000;
-- global variables
SHOW GLOBAL VARIABLES;
SET GLOBAL sort_buffer_size=1000000;
/etc/init.d/mysql stop
/usr/bin/mysqld_safe --skip-grant-tables &
mysql --user=root mysqlupdate user set Password=PASSWORD('new-password-here') WHERE User='root';
flush privileges;
fg
# (ctrl-c to kill mysql)
service mysql startalter table TABLENAME.COLUMN
add key (COLUMN(n));
show table status
like 'table_name' \G
-- Quick glance
show processlist ;
-- sort by user
select * from information_schema.processlist where user='foobar';
| Isolation level | dirty reads possible | nonrepeatable reads possible | phantom reads possible | locking reads |
|---|---|---|---|---|
| READ UNCOMMITTED | t | t | t | f |
| READ COMMITTED | f | t | t | f |
| REPEATABLE READ | f | f | t | f |
| SERIALIZABLE | f | f | f | t |
SET SESSION TRANSACTION ISOLATION LEVEL [level];
SHOW VARIABLES LIKE 'AUTOCOMMIT';
SET AUTOCOMMIT=[0|1]
check table [tablename]
repair table [tablename]
has high overhead, but row-level locking with multiversion concurrency control (MVCC)
uses table-locking, but is speedy
- Only uses INSERT and SELECT
- Compresses each new row with zlib
- Low disk I/O
- Ideal for logging
- Consists of data nodes, management nodes, and SQL nodes
- Real-time performance with redunancy and load-balancing capabilities
- Complex joins are slow, but single table lookups can be fast
- Uses MVCC, tries to keep transactions in memory
- (need to see where it's development is at now)
- similar to InnoDB
- Has high write concurrency
- (needs to be looked into)
- Avoid NULL when possible
optimize table
- Isolate the query column
- Try to simplify any math, and use literals when possible
- When indexing char columns, try using just a few letters
- good target is
count(distinct name) / count(*)
- good target is
-- see what time zones are in use
SELECT @@global.time_zone, @@session.time_zone;
-- set global time zone
SET GLOBAL time_zone = <timezone>;
-- set session time zone
SET time_zone = <timezone>;
mysqladmin COMMAND
| command | desc |
|---|---|
| flush-logs | rotate logs |
| version | get version |
select * from KEY_COLUMN_USAGE where REFERENCED_TABLE_NAME = 'tbl_name';set global super_read_only=0
superreadonly https://www.percona.com/blog/2016/09/27/using-the-super_read_only-system-variable/
- High Performance MySQL by Baron Schwartz, Perter Zaitsev, Vadim Tkachenko
-- session variables
SHOW SESSION VARIABLES;
SET SESSION sort_buffer_size=1000000;
-- global variables
SHOW GLOBAL VARIABLES;
SET GLOBAL sort_buffer_size=1000000;SELECT @@global.time_zone, @@session.time_zone;Port 3306 Plaintext protocol SSL is negotiated in-stream
- Isolate the query column
- Try to simplify any math, and use literals when possible
- When indexing char columns, try using just a few letters
- good target is
count(distinct name) / count(*)
- good target is
Enable this for debugging, don't leave it running if you don't need it.
set global slow_query_log = ON|OFF
set global slow_query_log_file = file_name
toolkit (https://www.percona.com/software/database-tools/percona-toolkit) pt-query-digest /var/log/mysql/mysql-slow.log
-- size in MB
select ENGINE, TABLE_NAME, Round(DATA_LENGTH/1024/1024) as data_length, round(INDEX_LENGTH/1024/1024) as index_length, round(DATA_FREE/1024/1024) as data_free, (data_free/(index_length+data_length)) as frag_ratio from information_schema.tables where DATA_FREE > 0 order by frag_ratio desc;
optimize table <tbl>;
https://dev.mysql.com/doc/refman/5.7/en/optimize-table.html
-
Use a query log to come up with a realistic workload that covers peek time and when batch jobs are run
-
Use fresh snapshots between benchmarks
-
Full stack tools:
-
ab
-
httpload
http_load -rate [requests_per_sec] -parallel [num_processes] -seconds [time] [url_file]
-
JMeter
-
-
Single component tools:
- Database Test Suite (made by OSDL)
- sql-bench
- Super Smack
Emulates client load
set profiling = 1;
* run query *
show profile;
- High Performance MySQL by Baron Schwartz, Perter Zaitsev, Vadim Tkachenko
DROP USER ''@'localhost';
DROP USER ''@'hostname';DELETE FROM mysql.db WHERE Db LIKE 'test%';
FLUSH PRIVILEGES;
DROP DATABASE test;For safe measure, don't just set the time zone in MySQL, but also set the system time zone to UTC
default-time-zone=UTC
[mysql]
prompt="(\U) [\d] >"
https://dev.mysql.com/doc/refman/5.6/en/mysql-commands.html
CHANGE MASTER TO
MASTER_HOST='example.com',
MASTER_LOG_FILE='mysqld-bin.123456',
MASTER_LOG_POS=123456;mysqlbinlog $BINLOG_FILEhttps://dev.mysql.com/doc/refman/5.6/en/replication-gtids-concepts.html
If the application is vulnerable to a file disclosure attack, it could allow attacker to download the DB files directly. This can be mitigated by proper file permissions, but a locked down ACL on a networked MySQL server is just as good (and makes the application more scalable as well).
If using InnoDB:
set global innodb_fast_shutdown=0
With a slow shutdown, InnoDB performs a full purge and change buffer merge before shutting down, which ensures that data files are fully prepared in case of file format differences between releases.
Run mysqlupgrade
mysqlupgrade should not be used when the server is running with --gtid-mode=ON. See GTID mode and mysqlupgrade for more information.
zfs set recordsize=16k tank/db
zfs set recordsize=128k tank/log
https://en.wikipedia.org/wiki/Aria_%28storage_engine%29
- No storage mechanism at all
- Useful for replication setups and audit logging
Reads, writes to CSV files
http://archive.oreilly.com/pub/a/databases/2006/08/10/mysql-federated-tables.html
innochecksum - offline file checksum utility
- uses table-level locking and lacks transactions, but has low overhead and is platform neutral
- excellent for read-only tables
myisamftdump A utility that displays information about full-text indexes in MyISAM tables. See Section 4.6.2, "myisamftdump --- Display Full-Text Index information".
myisamchk A utility to describe, check, optimize, and repair MyISAM tables. See Section 4.6.3, "myisamchk --- MyISAM Table-Maintenance Utility".
myisamlog A utility that processes the contents of a MyISAM log file. See Section 4.6.4, "myisamlog --- Display MyISAM Log File Contents".
myisampack A utility that compresses MyISAM tables to produce smaller read-only tables. See Section 4.6.5, "myisampack --- Generate Compressed, Read-Only MyISAM Tables"
mysqlhotcopy A utility that quickly makes backups of MyISAM tables while the server is running. See Section 4.6.10, "mysqlhotcopy --- A Database Backup Program"..
drop-in replacement for InnoDB binary compatibility with InnoDB database files