<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>database on seth-shi</title><link>https://sethix.com/tags/database/</link><description>Recent content in database on seth-shi</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Thu, 09 Jun 2022 16:59:31 +0800</lastBuildDate><atom:link href="https://sethix.com/tags/database/index.xml" rel="self" type="application/rss+xml"/><item><title>Optimizing Database Queries to Reduce MySQL Load to 1%</title><link>https://sethix.com/posts/2022060916/</link><pubDate>Thu, 09 Jun 2022 16:59:31 +0800</pubDate><guid>https://sethix.com/posts/2022060916/</guid><description> Following the previous Redis incident, our monitoring system recently alerted about abnormal MySQL load in the advertising system The Ops team asked me to investigate whether recent code changes caused the database load spike. As shown: Database The graph clearly shows a sudden load surge at the marked timestamp, which persisted without subsiding Checking Git commit history revealed no deployments on that day, indicating operational configuration changes might be the culprit Accessed Alibaba Cloud&amp;rsquo;s SQL Audit and Analysis feature (similar solutions exist on other cloud platforms): SQL Audit The problematic SQL query was quickly identified, accounting for over 90% of total queries Next steps were straightforward: Locate the corresponding code for this SQL Root cause analysis: Original code handled single configurations, but recent operational changes allowed multiple options No caching implementation Optimized query logic and added caching mechanisms Database Database Post-optimization showed reduced database load, but another SQL query became prominent with higher average latency due to WHERE EXISTS usage: The Laravel whereHas method generated inefficient WHERE EXISTS queries GitHub solutions suggested using WHERE IN instead Implemented custom solution using these patterns (alternative packages available): laravel-wherehasin hasin eloquent-has-by-non-dependent-subquery Database Database Final optimizations resulted in: Balanced query distribution Database load reduced to 1% (compared to ~50% previously) Consistent performance during peak periods</description></item><item><title>Optimizing Redis Memory Usage for Ad Service: Reducing 30G+ Memory Footprint</title><link>https://sethix.com/posts/2022031610/</link><pubDate>Wed, 16 Mar 2022 10:09:52 +0800</pubDate><guid>https://sethix.com/posts/2022031610/</guid><description>Problem Analysis While reviewing legacy code, we discovered Redis operations without TTL settings. This likely occurred because:
Hash structures can&amp;rsquo;t set expiration time during creation Subsequent developers forgot to add expiration Common problematic patterns:
Date-based Hash keys
HSET xxx:ad_id:20220315 userid value Orphaned ad keys
HSET xxx:ad_id userid value (after ad campaign ended) Initial cluster status: 8-node cluster with 8GB/node, but actual usage reached 51G+
Memory overconsumption forced continuous resource scaling</description></item><item><title>MySQL Partitioning Practice</title><link>https://sethix.com/posts/2021082715/</link><pubDate>Fri, 27 Aug 2021 15:46:03 +0800</pubDate><guid>https://sethix.com/posts/2021082715/</guid><description>While I&amp;rsquo;ve read many related articles before, true understanding comes from practice. Recently, the data team restructured logging formats. I designed a table schema and asked them to write cleaned client logs into it. As this is a video playback statistics table, we implemented date-based partitioning. With less than 100k videos in the library, we opted for monthly partitioning instead of daily. For auto-increment primary key id, you must create composite primary key with partitioning key In our actual implementation, we simply omitted the primary key `id` BIGINT UNSIGNED NOT NULL auto_increment, PRIMARY KEY(`id`, `date_key`) Partition by month Other playback statistics fields are omitted Records before 2021-09-01 will be stored in p_202108 partition Records from 2021-09-01 to 2021-09-30 will go to p_202109 partition CREATE TABLE `video_play_logs` ( `video_id` INT UNSIGNED NOT NULL, `date_key` date NOT NULL, index `video_play_logs_video_id_index` ( `video_id` ) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci PARTITION BY RANGE(to_days(`date_key`)) ( PARTITION p_202108 VALUES LESS THAN (to_days(&amp;#39;20210901&amp;#39;)), PARTITION p_202109 VALUES LESS THAN (to_days(&amp;#39;20211001&amp;#39;)), PARTITION p_202110 VALUES LESS THAN (to_days(&amp;#39;20211101&amp;#39;)), PARTITION p_202111 VALUES LESS THAN (to_days(&amp;#39;20211201&amp;#39;)), PARTITION p_202112 VALUES LESS THAN (to_days(&amp;#39;20220101&amp;#39;)) ) Verify partition configuration Insert test data and check TABLE_ROWS for record counts in each partition SELECT TABLE_NAME, PARTITION_NAME, TABLE_ROWS FROM information_schema.</description></item><item><title>Why Timestamp in MySQL Handles Time Zones Automatically</title><link>https://sethix.com/posts/2020120917/</link><pubDate>Wed, 09 Dec 2020 17:30:25 +0800</pubDate><guid>https://sethix.com/posts/2020120917/</guid><description>I&amp;rsquo;ve long been curious why MySQL&amp;rsquo;s timestamp type can effortlessly handle time zone issues. In practical use with Laravel frameworks, Migration files default to timestamp fields without requiring special attention.
Getting Started Check current database time zone settings mysql&amp;gt; show variables like &amp;#34;%time_zone%&amp;#34;; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | CST | | time_zone | +08:00 | +------------------+--------+ 2 rows in set (0.30 sec) View table structure mysql&amp;gt; desc timestamp_test; +--------------+-----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+-----------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | created_time | datetime | YES | | NULL | | | created_at | timestamp | YES | | NULL | | +--------------+-----------+------+-----+---------+----------------+ 3 rows in set (0.</description></item><item><title>MySQL Reverse Fuzzy Lookup</title><link>https://sethix.com/posts/2018040121/</link><pubDate>Sun, 01 Apr 2018 21:56:48 +0800</pubDate><guid>https://sethix.com/posts/2018040121/</guid><description>When developing a WeChat auto-reply demo, I encountered this scenario:
Receive specific messages Reply with predefined content based on keywords stored in database Requires partial matching with fuzzy database queries For conventional fuzzy matching, everyone knows to use LIKE. Given this table structure:
id keyword reply 1 David This is David 2 KingDavid This is David If the input keyword is &amp;ldquo;David&amp;rdquo;, we need to find all records containing David. The SQL would be:</description></item></channel></rss>