specific Query Performance Tips (see also database design tips for tips on indexes):
- Use EXPLAIN to profile the query execution plan
- Use Slow Query Log
(always have it on!)
- Don't use DISTINCT when you have or could use GROUP BY
- Insert performance
- Batch INSERT and REPLACE
- Use LOAD DATA instead of INSERT
- LIMIT m,n may not be as fast as it sounds
- Don't use ORDER BY RAND() if you have > ~2K records
- Use SQL_NO_CACHE when you are SELECTing frequently updated data or large sets of data
- Avoid wildcards at the start of LIKE queries
- Avoid correlated subqueries and in select and where clause (try to avoid in)
- No calculated comparisons -- isolate indexed columns
- ORDER BY and LIMIT work best with equalities and covered indexes
- Separate text/blobs from metadata, don't put text/blobs in results if you don't need them
- Derived tables (subqueries in the FROM clause) can be useful
for retrieving BLOBs without sorting them. (Self-join can speed up a
query if 1st part finds the IDs and uses then to fetch the rest)
- ALTER TABLE...ORDER BY can take data sorted chronologically
and re-order it by a different field -- this can make queries on that
field run faster (maybe this goes in indexing?)
- Know when to split a complex query and join smaller ones
- Delete small amounts at a time if you can
- Make similar queries consistent so cache is used
- Have good SQL query standards
- Don't use deprecated features
- Turning OR on multiple index fields (<5.0) into UNION may
speed things up (with LIMIT), after 5.0 the index_merge should pick
stuff up.
- Don't use COUNT * on Innodb tables for every search, do it a
few times and/or summary tables, or if you need it for the total # of
rows, use SQL_CALC_FOUND_ROWS and SELECT FOUND_ROWS()
- Use INSERT ... ON DUPLICATE KEY update (INSERT IGNORE) to avoid having to SELECT
- use groupwise maximum instead of subqueries
Scaling Performance Tips:
- Use benchmarking
- isolate workloads don't let administrative work interfere with customer performance. (ie backups)
- Debugging sucks, testing rocks!
- As your data grows, indexing may change (cardinality and
selectivity change). Structuring may want to change. Make your schema
as modular as your code. Make your code able to scale. Plan and embrace
change, and get developers to do the same.
Network Performance Tips:
- Minimize traffic by fetching only what you need.
- Paging/chunked data retrieval to limit
- Don't use SELECT *
- Be wary of lots of small quick queries if a longer query can be more efficient
- Use multi_query if appropriate to reduce round-trips
- Use stored procedures to avoid bandwidth wastage
OS Performance Tips:
- Use proper data partitions
- For Cluster. Start thinking about Cluster *before* you need them
- Keep the database host as clean as possible. Do you really need a windowing system on that server?
- Utilize the strengths of the OS
- pare down cron scripts
- create a test environment
- source control schema and config files
- for LVM innodb backups, restore to a different instance of MySQL so Innodb can roll forward
- partition appropriately
- partition your database when you have real data -- do not assume you know your dataset until you have real data
MySQL Server Overall Tips:
- innodb_flush_commit=0 can help slave lag
- Optimize for data types, use consistent data types. Use
PROCEDURE ANALYSE() to help determine the smallest data type for your
needs.
- use optimistic locking, not pessimistic locking. try to use shared lock, not exclusive lock. share mode vs. FOR UPDATE
- if you can, compress text/blobs
- compress static data
- don't back up static data as often
- enable and increase the query and buffer caches if appropriate
- config params -- http://docs.cellblue.nl/2007/03/17/easy-mysql-performance-tweaks/
is a good reference
- Config variables & tips:
- use one of the supplied config files
- key_buffer, unix cache (leave some RAM free), per-connection variables, innodb memory variables
- be aware of global vs. per-connection variables
- check SHOW STATUS and SHOW VARIABLES (GLOBAL|SESSION in 5.0 and up)
- be aware of swapping esp. with Linux, "swappiness" (bypass OS
filecache for innodb data files, innodb_flush_method=O_DIRECT if
possible (this is also OS specific))
- defragment tables, rebuild indexes, do table maintenance
- If you use innodb_flush_txn_commit=1, use a battery-backed hardware cache write controller
- more RAM is good so faster disk speed
- use 64-bit architectures
- --skip-name-resolve
- increase myisam_sort_buffer_size to optimize large inserts (this is a per-connection variable)
- look up memory tuning parameter for on-insert caching
- increase temp table size in a data warehousing environment
(default is 32Mb) so it doesn't write to disk (also constrained by
max_heap_table_size, default 16Mb)
- Run in SQL_MODE=STRICT to help identify warnings
- /tmp dir on battery-backed write cache
- consider battery-backed RAM for innodb logfiles
- use --safe-updates for client
- Redundant data is redundant
Storage Engine Performance Tips:
- InnoDB ALWAYS keeps the primary key as part of each index, so do not make the primary key very large
- Utilize different storage engines on master/slave ie, if you need fulltext indexing on a table.
- BLACKHOLE engine and replication is much faster than FEDERATED tables for things like logs.
- Know your storage engines and what performs best for your needs, know that different ones exist.
- ie, use MERGE tables ARCHIVE tables for logs
- Archive old data -- don't be a pack-rat! 2 common engines for this are ARCHIVE tables and MERGE tables
- use row-level instead of table-level locking for OLTP workloads
- try out a few schemas and storage engines in your test environment before picking one.
Database Design Performance Tips:
- Design sane query schemas. don't be afraid of table joins, often they are faster than denormalization
- Don't use boolean flags
- Use Indexes
- Don't Index Everything
- Do not duplicate indexes
- Do not use large columns in indexes if the ratio of SELECTs:INSERTs is low.
- be careful of redundant columns in an index or across indexes
- Use a clever key and ORDER BY instead of MAX
- Normalize first, and denormalize where appropriate.
- Databases are not spreadsheets, even though Access really really looks like one. Then again, Access isn't a real database
- use INET_ATON and INET_NTOA for IP addresses, not char or varchar
- make it a habit to REVERSE() email addresses, so you can
easily search domains (this will help avoid wildcards at the start of
LIKE queries if you want to find everyone whose e-mail is in a certain
domain)
- A NULL data type can take more room to store than NOT NULL
- Choose appropriate character sets & collations -- UTF16
will store each character in 2 bytes, whether it needs it or not,
latin1 is faster than UTF8.
- Use Triggers wisely
- use min_rows and max_rows to specify approximate data size so
space can be pre-allocated and reference points can be calculated.
- Use HASH indexing for indexing across columns with similar data prefixes
- Use myisam_pack_keys for int data
- be able to change your schema without ruining functionality of your code
- segregate tables/databases that benefit from different configuration variables
Other:
- Hire a MySQL (tm) Certified DBA
- Know that there are many consulting companies out there that can help, as well as MySQL's Professional Services.
- Read and post to MySQL Planet at http://www.planetmysql.org
- Attend the yearly MySQL Conference and Expo or other conferences with MySQL tracks (link to the conference here)
- Support your local User Group (link to forge page w/user groups here)
分享到:
相关推荐
OWASP-TOP10-2021 中文版V1.0.pdf 本资源为 OWASP-TOP10-2021 中文版V1.0.pdf,是一个关于 Web 应用程序安全的指南,旨在帮助开发者和安全专家识别和避免常见的安全风险。该资源涵盖了 OWASP-TOP10-2021 的项目介绍...
Top10 ProxyClient 使您可以: 1、通过代理服务器运行任何网络应用程序。对于软件不需要有什么特殊配置;整个过程是完全透明的。 2、可选择指定的进程进行上网,不影响未选择的进程,也可以强制所有网络连接,都通过...
### OWASP Top 10 漏洞详解 #### Top1:注入漏洞 **介绍:** 注入漏洞通常源于应用程序未能对用户输入进行适当的安全检查。这类漏洞允许攻击者通过发送包含命令的数据给解释器,使其作为有效命令被执行。...
【佐思汽研】2024汽车AI大模型TOP10分析报告.pdf【佐思汽研】2024汽车AI大模型TOP10分析报告.pdf【佐思汽研】2024汽车AI大模型TOP10分析报告.pdf【佐思汽研】2024汽车AI大模型TOP10分析报告.pdf【佐思汽研】2024汽车...
金融实践序列数据分析,量化投资用,机构持股清单;最新数据,更新到2019,第二季度
2019年文娱领域VC_公司投资数量TOP10.xls
### OWASP TOP10移动安全漏洞(安卓) #### 1. 脆弱的服务器端安全控制 在移动应用安全领域,“脆弱的服务器端安全控制”通常是指移动应用与服务器之间的交互缺乏足够的安全性。这种安全漏洞的核心在于移动应用在...
2019年淘宝汉服商家TOP10产值.xls
2020年中国饮料行业上市企业声誉TOP10.xls
owasp 测试指南 2008 3.0全册。高清版。需要的可以下载。
【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的竞赛项目学习资料,作为参考学习...基于python的超声波图像分割(神经细胞)参赛源码(top10%).zip
应对暑期实习笔试面试,最近整理了一些相关材料,此份思维导图权且帮助自己掌握知识。个人整理可能会有错误,请见谅。
2020中国房地产百强企业研究报告-中国房地产TOP10研究组-202003.rar
截至2018年10月中国短视频粉丝量TOP100美妆KOL粉丝TOP10城市分布情况.xlsx
2018年中国各类细分专业批发市场交易规模TOP10.xls
标题中的“TOP10宝贝情况表”暗示了这是一个关于排名前10的商品或产品的数据集,可能是电商领域的销售数据。由于文件格式为`.xls`,我们可以推断这是一份Excel电子表格,通常用于存储和分析结构化的数据。下面将详细...
OWASP IoT TOP10是该组织针对物联网设备可能面临的十大安全威胁的总结。接下来,本文将详细介绍OWASP IoT TOP10以及与物联网安全开发生命周期相关的知识点。 首先,OWASP IoT TOP10 2018年版列出了十大物联网安全...