mysql全文索引使用得到,对性能提升有一定帮助;但是,若使用不得到,将会是异常灾难;mysql全文索引对整个优化器的索引选择都有干扰。看我生产环境下优化过的一
mysql全文索引使用得到,对性能提升有一定帮助;但是,若使用不得到,将会是异常灾难;mysql全文索引对整个优化器的索引选择都有干扰。看我生产环境下优化过的一条sql
SELECT DISTINCT pc.products_id, pd.products_name,p.products_date_added,pso.products_id FROM products_to_categories AS pc LEFT JOIN products_description AS pd ON pd.products_id=pc.products_id LEFT JOIN products AS p ON p.products_id=pd.products_id LEFT JOIN specials AS sps ON sps.products_id=p.products_id LEFT JOIN temp_products_7days_orders_amount AS 7days ON 7days.products_id=pc.products_id LEFT JOIN products_realtime_quantity AS prq ON prq.sku_or_poa = p.products_model LEFT JOIN products_stockout AS pso ON pso.products_id=pd.products_id WHERE p.products_status=1 AND (prq.msg != 'Temporary out stock.' OR ISNULL(prq.msg)) AND pc.categories_id IN ( 153,323,1055,1241,1431) AND MATCH(pd.products_name) AGAINST('*iphone*' IN BOOLEAN MODE) AND MATCH(pd.products_name) AGAINST('*c*' IN BOOLEAN MODE) ORDER BY 7days.orders_sum DESC这条语句执行非常慢,经常出现卡住情况,有时候发现执行需要几分钟,而结果才几条,该语句也为涉及到大结果运算,各种连表条件上上都有索引。唯一特殊的地方就是pd.products_name为全文索引,而且执行的过程中pc.categories_id优先级高于pd.products_name全文索引,导致使用了pc.categories_id索引。按理来讲,这样也没有多大关系。但是explain后发现了问题
+----+-------------+-------+----------+-----------------------+-------------------+---------+---------------------------+------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+----------+-----------------------+-------------------+---------+---------------------------+------+----------------------------------------------+ | 1 | SIMPLE | pc | range | PRIMARY,categories_id | categories_id | 4 | NULL | 307 | Using where; Using temporary; Using filesort | | 1 | SIMPLE | pd | fulltext | PRIMARY,products_name | products_name | 0 | | 1 | Using where | | 1 | SIMPLE | p | eq_ref | PRIMARY | PRIMARY | 4 | banggood.pd.products_id | 1 | Using where | | 1 | SIMPLE | sps | ref | products_id | products_id | 4 | banggood.pd.products_id | 16 | Using index | | 1 | SIMPLE | 7days | ref | PRIMARY | PRIMARY | 4 | banggood.p.products_id | 1032 | | | 1 | SIMPLE | prq | ref | ix_prg_sku_or_poa | ix_prg_sku_or_poa | 152 | banggood.p.products_model | 10 | Using where | | 1 | SIMPLE | pso | eq_ref | PRIMARY | PRIMARY | 4 | banggood.pd.products_id | 1 | Using index | +----+-------------+-------+----------+-----------------------+-------------------+---------+---------------------------+------+----------------------------------------------+我们发现驱动表示pc表,使用了categories_id索引,可能优化器优先选择了它,但是再看pd表,
按理来讲,这个时候pd表应该使用products_id索引,也就是这个表的primary key,但是优化器却选择了products_name全文索引,坑爹了!
profiling这条语句,执行时间为2分钟以上
+-------------------------+------------+-----------+------------+--------------+---------------+ | Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out | +-------------------------+------------+-----------+------------+--------------+---------------+ | starting | 0.000415 | 0.000000 | 0.000000 | 0 | 0 | | checking permissions | 0.000011 | 0.000000 | 0.000000 | 0 | 0 | | checking permissions | 0.000004 | 0.000000 | 0.000000 | 0 | 0 | | checking permissions | 0.000002 | 0.000000 | 0.000000 | 0 | 0 | | checking permissions | 0.000003 | 0.000000 | 0.000000 | 0 | 0 | | checking permissions | 0.000056 | 0.001000 | 0.000000 | 0 | 0 | | checking permissions | 0.000006 | 0.000000 | 0.000000 | 0 | 0 | | checking permissions | 0.000009 | 0.000000 | 0.000000 | 0 | 0 | | Opening tables | 0.000225 | 0.000000 | 0.000000 | 0 | 0 | | System lock | 0.000029 | 0.000000 | 0.000000 | 0 | 0 | | init | 0.000138 | 0.000000 | 0.000000 | 0 | 0 | | optimizing | 0.000046 | 0.000000 | 0.000000 | 0 | 0 | | statistics | 0.001115 | 0.001000 | 0.000000 | 0 | 0 | | preparing | 0.001246 | 0.002000 | 0.000000 | 0 | 0 | | FULLTEXT initialization | 0.000088 | 0.000000 | 0.000000 | 0 | 0 | | Creating tmp table | 0.000057 | 0.000000 | 0.000000 | 0 | 0 | | executing | 0.000005 | 0.000000 | 0.000000 | 0 | 0 | | Copying to tmp table | 120.430834 | 81.227651 | 38.749110 | 1112 | 0 | | Sorting result | 0.000058 | 0.000000 | 0.000000 | 0 | 0 | | Sending data | 0.000026 | 0.000000 | 0.000000 | 0 | 0 | | end | 0.000007 | 0.000000 | 0.000000 | 0 | 0 | | removing tmp table | 0.000015 | 0.000000 | 0.000000 | 0 | 0 | | end | 0.000041 | 0.001000 | 0.000000 | 0 | 0 | | query end | 0.000007 | 0.000000 | 0.000000 | 0 | 0 | | closing tables | 0.000023 | 0.000000 | 0.000000 | 0 | 0 | | freeing items | 0.008546 | 0.000000 | 0.007999 | 0 | 0 | | logging slow query | 0.000008 | 0.000000 | 0.000000 | 0 | 0 | | logging slow query | 0.000007 | 0.000000 | 0.000000 | 0 | 0 | | cleaning up | 0.000008 | 0.000000 | 0.000000 | 0 | 0 | +-------------------------+------------+-----------+------------+--------------+---------------+看到Copying to tmp table占据了大量的cpu运算。
看来,mysql优化器太弱了,又要我们强制使用索引了!force index(primary) ,强制使用pd表的主键

MySQL和SQLite的主要区别在于设计理念和使用场景:1.MySQL适用于大型应用和企业级解决方案,支持高性能和高并发;2.SQLite适合移动应用和桌面软件,轻量级且易于嵌入。

MySQL中的索引是数据库表中一列或多列的有序结构,用于加速数据检索。1)索引通过减少扫描数据量提升查询速度。2)B-Tree索引利用平衡树结构,适合范围查询和排序。3)创建索引使用CREATEINDEX语句,如CREATEINDEXidx_customer_idONorders(customer_id)。4)复合索引可优化多列查询,如CREATEINDEXidx_customer_orderONorders(customer_id,order_date)。5)使用EXPLAIN分析查询计划,避

在MySQL中使用事务可以确保数据一致性。1)通过STARTTRANSACTION开始事务,执行SQL操作后用COMMIT提交或ROLLBACK回滚。2)使用SAVEPOINT可以设置保存点,允许部分回滚。3)性能优化建议包括缩短事务时间、避免大规模查询和合理使用隔离级别。

选择PostgreSQL而非MySQL的场景包括:1)需要复杂查询和高级SQL功能,2)要求严格的数据完整性和ACID遵从性,3)需要高级空间功能,4)处理大数据集时需要高性能。PostgreSQL在这些方面表现出色,适合需要复杂数据处理和高数据完整性的项目。

MySQL数据库的安全可以通过以下措施实现:1.用户权限管理:通过CREATEUSER和GRANT命令严格控制访问权限。2.加密传输:配置SSL/TLS确保数据传输安全。3.数据库备份和恢复:使用mysqldump或mysqlpump定期备份数据。4.高级安全策略:使用防火墙限制访问,并启用审计日志记录操作。5.性能优化与最佳实践:通过索引和查询优化以及定期维护兼顾安全和性能。

如何有效监控MySQL性能?使用mysqladmin、SHOWGLOBALSTATUS、PerconaMonitoringandManagement(PMM)和MySQLEnterpriseMonitor等工具。1.使用mysqladmin查看连接数。2.用SHOWGLOBALSTATUS查看查询数。3.PMM提供详细性能数据和图形化界面。4.MySQLEnterpriseMonitor提供丰富的监控功能和报警机制。

MySQL和SQLServer的区别在于:1)MySQL是开源的,适用于Web和嵌入式系统,2)SQLServer是微软的商业产品,适用于企业级应用。两者在存储引擎、性能优化和应用场景上有显着差异,选择时需考虑项目规模和未来扩展性。

在需要高可用性、高级安全性和良好集成性的企业级应用场景下,应选择SQLServer而不是MySQL。1)SQLServer提供企业级功能,如高可用性和高级安全性。2)它与微软生态系统如VisualStudio和PowerBI紧密集成。3)SQLServer在性能优化方面表现出色,支持内存优化表和列存储索引。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)