SQL tips

  1. Use EXPLAIN to profile the query execution plan
  2. Use Slow Query Log (always have it on!)
  3. Don't use DISTINCT when you have or could use GROUP BY
  4. Insert performance
    1. Batch INSERT and REPLACE
    2. Use LOAD DATA instead of INSERT
  5. LIMIT m,n may not be as fast as it sounds. Learn how to improve it and read more about Efficient Pagination Using MySQL
  6. Don't use ORDER BY RAND() if you have > ~2K records
  7. Use SQL_NO_CACHE when you are SELECTing frequently updated data or large sets of data
  8. Avoid wildcards at the start of LIKE queries
  9. Avoid correlated subqueries and in select and where clause (try to avoid in)
  10. No calculated comparisons -- isolate indexed columns
  11. ORDER BY and LIMIT work best with equalities and covered indexes
  12. Separate text/blobs from metadata, don't put text/blobs in results if you don't need them
  13. 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)
  14. 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?)
  15. Know when to split a complex query and join smaller ones
  16. Delete small amounts at a time if you can
  17. Make similar queries consistent so cache is used
  18. Have good SQL query standards
  19. Don't use deprecated features
  20. 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.
  21. 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()
  22. Use INSERT ... ON DUPLICATE KEY update (INSERT IGNORE) to avoid having to SELECT
  23. use groupwise maximum instead of subqueries
  24. Avoid using IN(...) when selecting on indexed fields, It will kill the performance of SELECT query.
  25. Prefer using UNION ALL if you don't need to merge the result
Scaling Performance Tips:
  1. Use benchmarking
  2. isolate workloads don't let administrative work interfere with customer performance. (ie backups)
  3. Debugging sucks, testing rocks!
  4. 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:
  1. Minimize traffic by fetching only what you need.
    1. Paging/chunked data retrieval to limit
    2. Don't use SELECT *
    3. Be wary of lots of small quick queries if a longer query can be more efficient
  2. Use multi_query if appropriate to reduce round-trips
  3. Use stored procedures to avoid bandwidth wastage
OS Performance Tips:
  1. Use proper data partitions
    1. For Cluster. Start thinking about Cluster *before* you need them
  2. Keep the database host as clean as possible. Do you really need a windowing system on that server?
  3. Utilize the strengths of the OS
  4. pare down cron scripts
  5. create a test environment
  6. source control schema and config files
  7. for LVM innodb backups, restore to a different instance of MySQL so Innodb can roll forward
  8. partition appropriately
  9. partition your database when you have real data -- do not assume you know your dataset until you have real data
  10. Reduce swappiness of your OS
MySQL Server Overall Tips:
  1. innodb_flush_commit=0 can help slave lag
  2. Optimize for data types, use consistent data types. Use PROCEDURE ANALYSE() to help determine the smallest data type for your needs.
  3. use optimistic locking, not pessimistic locking. try to use shared lock, not exclusive lock. share mode vs. FOR UPDATE
  4. if you can, compress text/blobs
  5. compress static data
  6. don't back up static data as often
  7. enable and increase the query and buffer caches if appropriate
  8. config params -- http://docs.cellblue.nl/2007/03/17/easy-mysql-performance-tweaks/ is a good reference
  9. Config variables & tips:
    1. use one of the supplied config files
    2. key_buffer, unix cache (leave some RAM free), per-connection variables, innodb memory variables
    3. be aware of global vs. per-connection variables
    4. check SHOW STATUS and SHOW VARIABLES (GLOBAL|SESSION in 5.0 and up)
    5. 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))
    6. defragment tables, rebuild indexes, do table maintenance
    7. If you use innodb_flush_txn_commit=1, use a battery-backed hardware cache write controller
    8. more RAM is good so faster disk speed
    9. use 64-bit architectures
  10. --skip-name-resolve
  11. increase myisam_sort_buffer_size to optimize large inserts (this is a per-connection variable)
  12. look up memory tuning parameter for on-insert caching
  13. 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)
  14. Run in SQL_MODE=STRICT to help identify warnings
  15. /tmp dir on battery-backed write cache
  16. consider battery-backed RAM for innodb logfiles
  17. use --safe-updates for client
  18. Redundant data is redundant
  19. Keep an eye on buffer pool and keybuffer hit rate
Storage Engine Performance Tips:
  1. InnoDB ALWAYS keeps the primary key as part of each index, so do not make the primary key very large
  2. Utilize different storage engines on master/slave ie, if you need fulltext indexing on a table.
  3. BLACKHOLE engine and replication is much faster than FEDERATED tables for things like logs.
  4. Know your storage engines and what performs best for your needs, know that different ones exist.
    1. ie, use MERGE tables ARCHIVE tables for logs
    2. Archive old data -- don't be a pack-rat! 2 common engines for this are ARCHIVE tables and MERGE tables
  5. use row-level instead of table-level locking for OLTP workloads
  6. try out a few schemas and storage engines in your test environment before picking one.
Database Design Performance Tips:
  1. Design sane query schemas. don't be afraid of table joins, often they are faster than denormalization
  2. Don't use boolean flags
  3. Use Indexes
  4. Don't Index Everything
  5. Do not duplicate indexes
  6. Do not use large columns in indexes if the ratio of SELECTs:INSERTs is low.
  7. Split out large blob elements in InnoDB
  8. be careful of redundant columns in an index or across indexes
  9. Use a clever key and ORDER BY instead of MAX
  10. Normalize first, and denormalize where appropriate.
  11. Databases are not spreadsheets, even though Access really really looks like one. Then again, Access isn't a real database
  12. use INET_ATON and INET_NTOA for IP addresses, not char or varchar
  13. 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)
  14. A NULL data type can take more room to store than NOT NULL
  15. Avoid NULL in index attributes. Use 0 instead
  16. Storing flags in a database can slow down execution due to a bad cardinality. Try using bit flags
  17. Don't store flags in a NULL and NOT NULL manner. Update from NULL -> 1 is slower than 0 -> 1
  18. Choose appropriate character sets & collations -- UTF16 will store each character in 2 bytes, whether it needs it or not, latin1 is faster than UTF8.
  19. Use Triggers wisely
  20. Use delayed key wrote
  21. use min_rows and max_rows to specify approximate data size so space can be pre-allocated and reference points can be calculated.
  22. Use HASH indexing for indexing across columns with similar data prefixes
  23. Use myisam_pack_keys for int data
  24. be able to change your schema without ruining functionality of your code
  25. segregate tables/databases that benefit from different configuration variables
  26. Don't access the last key part in a where clause with =
  27. Abuse the system for optimiization you're using with system dependant features like RTREE's for optimized range queries

Comments

Related Posts Plugin for WordPress, Blogger...

Popular Posts

மூக்குத்தி அணிவது ஏன்?

UI Certifications Q & A

Technicals details select

Do's and Don'ts - Central Pollution Control Board (CPCB),

for programmers dropdown

medicals dropdown

:: Useful web links List

  • Velliangiri herbals - வெள்ளியங்கிரி ஹெர்பல்ஸ் வழங்கும் இயற்கை முறையில் விளைந்த, வீட்டு முறையில் தயாரிக்கப்பட்ட புரத சத்து நிறைந்த ❤️முருங்கை இலை,கருவேப்பிலை,குறு ...
  • Election 2024 - [image: ELECTION DATES] [image: SUN TV]
  • Beware of Fake applications & Fake Whatsapp groups - *Caution Notice* *Attention to all Customers and the General Public* Dear Client, It has come to our notice that some unknown individuals have been fra...
  • web blogs - best blog links http://tv-actors.blogspot.in/ http://bedtti.blogspot.com/ http://firstnightdecoration.blogspot.com/ http://itparkcareers.blogspot.com/ http:...
  • IFHRMS Wipro Error - அனைத்து அரசு அலுவலகங்களிலும் IFHRMS என்ற செயலியின் மூலம் பணம் சார்ந்த பட்டியல்கள் தயாரிக்கப்படுகிறது. ஆனால் இம்மாதம் மார்ச் மாதத்தில் இருந்து IFHRMS செய...
  • soap - 100% organic Home made soap product Goat milk – 1. improve dry skin 2. healthy skin microbiome 3. maintain the skin’s moisture Charcoal soap 1....
  • blogs - http://tv-actors.blogspot.in/ http://bedtti.blogspot.com/ http://firstnightdecoration.blogspot.com/ http://itparkcareers.blogspot.com/ http://kottapathar.blo...
  • ELDERS HOME - ELDERS HOME IN MADURAI! Old age home in mela anuppanadi(near Thepakulam) *Individual *and double sharing rooms A/C,non A/C rooms 24 hrs staff support,cot,...
  • Old Games in Tamilnadu - *1.Kabaddi* [image: istockphoto-1790754800-612x612.jpg]Tamil Nadu is home to the popular team sport of Kabaddi. Kabaddi is the state sport of Tamil Na...
  • Samiyana Pandhal - Contact For Chairs & Samiyana Pandhal Prop: S.Muthuraman *MJM Samiyana & Chair Land* 9-1, Madurai Main Road, 4th Street Sivagangai, Tamilnadu Cell: +91 90...
  • Exclusive market updates - Inbox Search for all messages with label Inbox Remove label Inbox from this conversation data collections Search for all messages with label data collect...
  • FAQ - What is E.S.I Scheme? In addition to necessities of food, clothing, housing etc., man needs security in times of physical and economic distress conseq...
  • IFHRMS Wipro Error - அனைத்து அரசு அலுவலகங்களிலும் IFHRMS என்ற செயலியின் மூலம் பணம் சார்ந்த பட்டியல்கள் தயாரிக்கப்படுகிறது. ஆனால் இம்மாதம் மார்ச் மாதத்தில் இருந்து IFHRMS செயல...
  • Wedding Seervarisai Thattu decoration models - [image: sevajothi-plate-decorators] [image: sevajothi-plate-decorators] [image: sevajothi-plate-decorators] [image: sevajothi-plate-decorators] [imag...
  • Plots for sale - 5 வருசத்துல 4 மடங்கு விலை ! இப்ப இடம் வாங்கலைன்னா..... எப்ப வாங்க போறீங்க? உங்க பட்ஜெட்டில் ஒரு கனவு வீடு சாத்தியமா ? சாத்தியம் இனி வாடகை வீட்டுக்கு By...
  • Contact Us - Sevajothi Trust 45, TEACHERS QUATERS MELUR - 625106 MADURAI DT TAMILNADU MOBILE: 7708661011, WHATSAPP: 7708661011, https://sevajothi.blogspot.co...
  • IFHRMS Wipro Error - அனைத்து அரசு அலுவலகங்களிலும் IFHRMS என்ற செயலியின் மூலம் பணம் சார்ந்த பட்டியல்கள் தயாரிக்கப்படுகிறது. ஆனால் இம்மாதம் மார்ச் மாதத்தில் இருந்து IFHRMS செய...
  • INTERNSHIPS - GOVERNMENT: https://www.niti.gov.in/internship https://internship.mea.gov.in/internship https://dpiit.gov.in/internship-scheme-0 https://www.shar.gov.in/...
  • News - நாள்- 29-07-2024 வருகின்ற திங்கட்கிழமை நேரம் -காலை 11மணி இடம்..திருமங்கலம் தெற்குதெரு அகமுடையார் சங்க மஹால்..... தேவரின அரசாணை சம்பந்தமாக ஆலோசனை கூட்ட...
  • ads links - https://tnpsc-ssc-rrb-tet-trb-bsrb.blogspot.com/ https://trivandrumtomorrow.blogspot.com/ https://womens-trust.blogspot.com/ https://madurai-kamaraj-univer...
  • Tags - Tags, lorry, truck, booking, LBS, parcel service, transport, logistics, van, container,
  • links - *Education**:* http://bedtti.blogspot.com/ http://indiansportsgames.blogspot.in/ http://interfacetrivandrum.blogspot.in/ http://deoceo.blogspot.com/ ...
  • For Rent - House for lease or rent at moderate rates. Location; Melur, Madurai 1 & 2 bedrooms nice new house in compound contact: Prabha - 8525913174
  • Marriage links - bride groom background verification online https://sevajothitechnologies.blogspot.com/ MJM Samiyana Chair Land Hiring Services - Decorators in Sivagang...
  • ELDERS HOME - ELDERS HOME IN MADURAI! Old age home in mela anuppanadi(near Thepakulam) *Individual *and double sharing rooms A/C,non A/C rooms 24 hrs staff support,cot,f...
  • Keywords - bus, bus day, transport, SETC, PRC, KSRTC, TNSTC, airways, airbus, car, train, truck, Ship, Boat, Electric Trian, Locomotive, passenger, public transport, ...
  • Dr.Radhakrishnan Best Teacher Award - https://nationalawardstoteachers.education.gov.in/ https://www.awards.gov.in/ Conditions of Eligibility of Teachers - School teachers and Heads of ...
  • Free food - Free food by MJM Chairland at Sivagangai Velayithasamy koil function
  • links ad content - www.standardtips.com = links URL = www.standardtips.com Title = All tips and tricks for your complete life. Description = Get the complete tips fo...
  • social links - https://gtechindia.org/member/softland-india-ltd https://www.tradeindia.com/softland-india-ltd-29664352/ https://in.linkedin.com/company/softland-india...
  • IFHRMS - *Danger:*Due to inactivity, your session has expired. Before you continue, make sure your entered data is saved locally.
  • Hardware links - http://softlandindia.blogspot.in/ http://indianindustrycompanies.blogspot.in/2011/06/wireless-electronic-spot-billing-and.html http://designersiva.blogspot.i...
  • quotation - Dt: 24.12.2007 To, Mr.Kumerasen www.dravidar.org *Sub:- AMC Agreement for www.dravidar.org* Sir, As per our earlier ...
  • design blogs - https://graphicsbackgrounds.blogspot.in/ https://designersiva.blogspot.in/ http://myclientsmydesigns.blogspot.com/ http://beamconsultancy.blogspot.com/ http...
  • Joint Venture (JV) Deal - What is a Joint Venture (JV) Deal? Off late demand for joint ventures in real estate has been increasing exponentially. In real estate, JV means that a ...
  • கீரைகளும்_அதன் முக்கிய_பயன்களும்: - 🌿அகத்திக்கீரை- ரத்தத்தை சுத்தமாக்கி பித்தத்தை தெளியவைக்கும். 🌿காசினிக்கீரை- சிறுநீரகத்தை நன்கு செயல்பட வைக்கும். உடல் வெப்பத்தை தணிக்கும். 🌿சிறுபசலைக்கீ...
  • blog links - *Education*: http://bedtti.blogspot.com/ http://staff-teachers-students.blogspot.in/ http://indiansportsgames.blogspot.in/ http://svpitmcbe.blogspot.com/ ht...
  • திருக்குறள்: - 1. கற்க கசடறக் கற்பவை கற்றபின் நிற்க அதற்குத் தக. 2. அரியவற்று ளெல்லாம் அரிதே பெரியாரைப் பேணித் தமராக் கொளல். -திருவள்ளுவர் குறள் விளக்கம்: பெரியாரைப் போற்...
  • top 10 female social media influencers in india - *Larissa D'Sa* *Travel Influencer* *7 lakh Instagram followers* *593K YouTube subscribers* [image: Larissa D'Sa] *IMAGE: Travel entrepreneur Larissa D'...
  • shopping links - vikas nighties shopping links https://www.meesho.com/vikas-cotton-nighties/p/zev2g?srsltid=AfmBOopSIFB5et-WM3zDmotr8kmL-hy-QcAggELxCe9THJKZXrnblGRr http...
  • Site Map - Companies - arasappalams (1) - Bharath Foundation (1) - conveytechniques (1) - links (1) - MEETPPU (1) - MR GROUP (1) - MTN Colle...
  • ads links - https://tnpsc-ssc-rrb-tet-trb-bsrb.blogspot.com/ https://trivandrumtomorrow.blogspot.com/ https://womens-trust.blogspot.com/ https://madurai-kamaraj-univer...