| 首页| 战狼2| 飞龙在天| 锦衣卫| 霍去病| 顶级厨师| 新闻编辑室| 石评大财经|
您的位置:首页 > 新闻中心 > 社会 > 正文

超级战舰

网站数据库优化:MySQL调优和缓存策略_我的网站

天眼

A |     Ahead of Christmas, Rashmika Mandanna went on an overseas holiday. And she took to social media and shared glimpses from her festive vacation. Dressed in a long black coat paired with a top and skirt, Rashmika gave a glimpse into her fun-filled getaway and cheerful moments as she struck a pose in front of a Christmas tree. Rashmika dropped a few photos and captioned them, “Merry Christmas my loves!"Earlier, Rashmika Mandanna and Vijay Deverakonda reportedly jetted off from Hyderabad together for vacation. Buzz is that they would also spend the New Year together. Both the actors took to their respective social media accounts and shared photos from the airport. Rashmika and Vijay will be seen together in an upcoming Telugu film, a rustic period drama, under director Rahul Sankrityan. This will be the third time they share screen space after Geetha Govindam and Dear Comrade.。    

     一键部署OpenClaw        网站慢,很多时候不是带宽不够,而是数据库查询慢。优化MySQL其实就两件事:加索引和用缓存。    先说索引。

B | 没索引的查询就是全表扫描,10万条数据能查出你服务器CPU飙到100%。加了索引,查询速度能提升百倍。

C |     -- MySQL索引优化示例    -- 查看慢查询(找出需要优化的SQL)    SELECT * FROM mysql.slow_log    WHERE start_time > NOW() - INTERVAL 1 DAY    ORDER BY query_time DESC LIMIT 10;    -- 用EXPLAIN分析查询计划    EXPLAIN SELECT * FROM articles    WHERE category_id = 5 AND status = 1    ORDER BY created_at DESC LIMIT 20;    -- 如果type是ALL(全表扫描),就需要加索引    CREATE INDEX idx_category_status_time    ON articles(category_id, status, created_at);    -- 复合索引遵循最左前缀原则    -- 即:查询条件从左到右依次命中索引    再说缓存。领用最多的场景是Redis缓存。把查询结果存到Redis里,下次查询直接从Redis取,不用查数据库。    # Redis缓存示例(PHP + Redis)    // 先查缓存    $redis = new Redis();    $redis->connect('127.0.0.1', 6379);    $cacheKey = 'article_list_page_' . $page;    $cached = $redis->get($cacheKey);    if ($cached) {    // 缓存命中,直接返回    echo $cached;    } else {    // 缓存未命中,查数据库    $articles = $pdo->query("SELECT * FROM articles ORDER BY id DESC LIMIT 20")->fetchAll();    $html = renderArticles($articles);    // 存入缓存,过期10分钟    $redis->setex($cacheKey, 600, $html);    echo $html;    }    MySQL本身也有查询缓存,在my.cnf里开启就行。但注意,数据更新后缓存不会立即失效,对实时性要求高的场景不适合。    # MySQL查询缓存配置(my.cnf)    query_cache_type = 1    query_cache_size = 64M    query_cache_limit = 2M    # InnoDB缓冲池大小(一般设为内存的70%)    innodb_buffer_pool_size = 2G    innodb_log_file_size = 256M    最后,定期清理数据库碎片和日志。数据量大了,碎片会导致查询变慢。

D | 每月跑一次OPTIMIZE TABLE就能解决。

E |

        
    

申请创业报道,分享创业好点子。点击此处,共同探讨创业新机遇!。

Current article:http://6icev.linxiuhuaishuangniuzhenrua.bond/dhju0dl/20260826/5050835.html

Published on:13:56:25


24小时排行

热点推荐

更多>>

图片精选