DesignImage.from.available.audited_with_colors
# ALWAYS used
tar = DesignImage.from.available.audited_with_colors.first
> DesignImage Load (9.3ms) SELECT `design_images`.* FROM `design_images` WHERE (design_images.created_at > ('2013-3-1')) AND (design_images.imageable_id is not null and design_images.imageable_type is not null and design_images.imageable_type <> 'Inspiration' and design_images.user_id is not null) AND (edited_color = 1 and audited = 1) LIMIT 1
======================
考虑建立联合索引
检查design_images.imageable_type <> 'Inspiration'的稀疏程度
#ruby
has = {all:0, on:0, off:0}
has[:all] = DesignImage.where('design_images.created_at > "2013-03-01"').count
DesignImage.where('design_images.created_at > "2013-03-01"').find_each{|e| has[:off] = has[:off] + 1 if e.imageable_type != 'Inspiration' }
has[:on] = has[:all] - has[:off]
has
=> {:all=>132099, :on=>245, :off=>131854}
结论:
绝大部分都是imageable_type <> 'Inspiration'
建立联合索引 edited_color,audited,created_at
======================
mysql> show index from design_images;
+---------------+---------------------------------------+--------------+-----------------+-----------+-------------+------+------------+
| Table | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Null | Index_type |
+---------------+---------------------------------------+--------------+-----------------+-----------+-------------+------+------------+
| design_images | PRIMARY | 1 | id | A | 176810 | | BTREE |
| design_images | NewIndex1 | 1 | user_id | A | 19645 | YES | BTREE |
| design_images | NewIndex4 | 1 | is_cover | A | 176810 | YES | BTREE |
| design_images | NewIndex5 | 1 | created_at | A | 176810 | | BTREE |
| design_images | index_design_images_on_file_file_size | 1 | file_file_size | A | 176810 | YES | BTREE |
| design_images | index_design_images_on_imageable_id | 1 | imageable_id | A | 35362 | YES | BTREE |
| design_images | index_design_images_on_is_cover | 1 | is_cover | A | 176810 | YES | BTREE |
| design_images | sorts | 1 | sorts | A | 6 | YES | BTREE |
| design_images | audited | 1 | audited | A | 2 | YES | BTREE |
| design_images | edited_color | 1 | edited_color | A | 2 | YES | BTREE |
| design_images | area_id | 1 | area_id | A | 58936 | YES | BTREE |
| design_images | index_design_images_on_timestamp | 1 | created_at | A | 176810 | | BTREE |
| design_images | index_design_images_on_timestamp | 2 | file_file_name | A | 176810 | YES | BTREE |
| design_images | index_design_images_on_timestamp | 3 | file_updated_at | A | 176810 | YES | BTREE |
| design_images | index_design_images_on_imageable_type | 1 | imageable_type | A | 2996 | YES | BTREE |
| design_images | index_design_images_on_title | 1 | title | A | 44202 | YES | BTREE |
| design_images | count_index | 1 | created_at | A | 176810 | | BTREE |
| design_images | count_index | 2 | imageable_id | A | 176810 | YES | BTREE |
| design_images | count_index | 3 | imageable_type | A | 176810 | YES | BTREE |
| design_images | count_index | 4 | user_id | A | 176810 | YES | BTREE |
| design_images | count_index | 5 | edited_color | A | 176810 | YES | BTREE |
| design_images | count_index | 6 | audited | A | 176810 | YES | BTREE |
+---------------+---------------------------------------+--------------+-----------------+-----------+-------------+------+------------+
22 rows in set (0.00 sec)
add 1 => [audited, edited_color, created_at]
del 4 => count_index, audited, edited_color, index_design_images_on_timestamp
add 1 => updated_at
del 4 => index_design_images_on_file_file_size, index_design_images_on_is_cover, NewIndex4, sorts
add 1 => no_audited
db/migrate/20140716104927_remove_index_of_design_images.rb
class RemoveIndexOfDesignImages < ActiveRecord::Migration # >show index from design_images; # # add 1 => [audited, edited_color, created_at] # del 4 => count_index, audited, edited_color, index_design_images_on_timestamp # add 1 => updated_at # del 4 => index_design_images_on_file_file_size, index_design_images_on_is_cover, NewIndex4, sorts # add 1 => no_audited def up remove_index :design_images, name:'count_index' remove_index :design_images, name:'audited' remove_index :design_images, name:'edited_color' remove_index :design_images, name:'index_design_images_on_timestamp' remove_index :design_images, :file_file_size remove_index :design_images, :is_cover remove_index :design_images, name:'NewIndex4' remove_index :design_images, name:'sorts' add_index :design_images, [:audited,:edited_color,:created_at], name:'from_available_audited_with_colors' add_index :design_images, :updated_at add_index :design_images, :no_audited end def down remove_index :design_images, name:'from_available_audited_with_colors' remove_index :design_images, :updated_at remove_index :design_images, :no_audited add_index :design_images, [:created_at,:imageable_id,:imageable_type,:user_id,:edited_color,:audited], name:'count_index' add_index :design_images, :audited, name:'audited' add_index :design_images, :edited_color, name:'edited_color' add_index :design_images, [:created_at,:file_file_name,:file_updated_at], name:'index_design_images_on_timestamp' add_index :design_images, :file_file_size add_index :design_images, :is_cover add_index :design_images, :is_cover, name:'NewIndex4' add_index :design_images, :sorts, name:'sorts' end end
http://guides.rubyonrails.org/v3.2.15/migrations.html#using-the-change-method
针对有名字的索引进行删除 remove_index :design_images, name:'count_index'
remove_index方法没有在支持change的函数列表中,所以migration需要写up和down
相关推荐
4. **索引查看**:Rails DB可以显示数据库表的索引信息,帮助开发者了解数据的索引情况,优化查询性能。 5. **数据库迁移管理**:它可以显示数据库的迁移历史,方便跟踪数据库结构的变化。 6. **多数据库支持**:...
- **性能优化**:利用缓存机制、数据库索引等方式提升应用性能。 #### 七、Rails 4中的安全措施 - **安全辅助方法**:如`params.permit`避免参数篡改。 - **加密与哈希**:使用BCrypt等库对密码进行加密存储。 - *...
1. **索引优化**:数据库索引是提升查询速度的关键。Rails虽然自动为`id`字段创建索引,但其他用于频繁搜索的字段也需要手动添加索引。例如,在`ChangingThePresent.org`的例子中,`login`、`email`、`first_name`和...
9. **性能优化**:由于Oracle和Rails的组合可能涉及更多的性能调优,例如使用连接池、预编译的SQL语句和数据库索引。 10. **错误处理和调试**:在开发过程中,可能会遇到兼容性问题或错误,因此了解如何调试和解决...
5. **数据库设计**:理解数据库的基本概念,如ER模型、索引、JOIN操作等,以及如何在MySQL中创建和操作表。 6. **服务器端开发**:如果使用Node.js作为后端,需要了解Express框架,或者其他后端技术如Django或Ruby ...
此外,联表查询和索引优化可能需要额外的考虑,以确保性能不会受到显著影响。 总的来说,Apartment为Rails开发者提供了一个强大的工具,帮助他们在单个数据库中有效地管理多个租户的数据。通过合理配置和使用,可以...
- 对于后端处理,可以使用Node.js、Django或Ruby on Rails等服务器端框架,结合数据库(如MySQL、MongoDB)来存储和查询城市数据。 - 使用Ajax或Fetch API进行异步请求,实现实时更新热门城市列表。 - 为了优化性能...
它可以帮助开发者在MySQL数据库上执行常见的数据库操作,如创建、更新和删除表,以及处理索引和外键等复杂结构。 在实际应用中,Ruby-Shift可能包含以下特性: 1. **命令行接口(CLI)**:提供一系列命令,使...
在模型中需要一个时间戳字段(例如created_at或updated_at),并在上面带有数据库索引 变更日志 安装 步骤1:将其添加到您的Gemfile中: gem 'unread' 并运行 bundle 步骤2:生成并运行迁移: rails g unread:...
例如,它可能利用了MySQL特有的索引、存储过程、触发器等功能,而这些在其他数据库中可能有不同的实现或者不存在。这就意味着,如果想在其他数据库上使用相同的功能,可能需要寻找其他的ORM框架,或者手动进行适配。...
5. 了解并掌握如何处理Rails与Oracle之间的性能优化问题,如索引策略和查询优化。 总之,Ruby on Rails提供了快速开发Web应用的工具,而Oracle则提供了稳定且强大的数据库支持。虽然两者结合使用可能需要额外的配置...
可能涉及SQL查询优化,索引使用,表设计,以及数据库的并发处理能力。 2. **performance_analysis_mysql.sql** - 类似地,这个文件可能是针对MySQL数据库的性能分析脚本,可能涵盖了如何提升MySQL查询速度,存储...
ActiveRecord MySQL 适配器的迁移添加列注释和索引注释安装将此行添加到应用程序的 Gemfile 中: gem 'activerecord-mysql-comment' 然后执行: $ bundle或者自己安装: $ gem install activerecord-mysql-comment...
自述文件辅导员派遣中心网站学生方的职能功能1您可以找到与您的学生相...教师桌柱子类型选项名称弦(191) null:false,索引:true 解释解释文本null:假风格整数null:假州整数null:假性别细绳null:假职业细绳nul
在Rails应用中集成MongoDB,可以为开发者提供一种不同于传统关系型数据库如MySQL或PostgreSQL的选择。 MongoDB的主要特点包括: 1. **文档型数据模型**:MongoDB以JSON-like格式存储数据,称为BSON(Binary JSON)...
总的来说,"语言后端:Lin-guage应用程序的Ruby on Rails数据库"涉及的内容包括但不限于Ruby on Rails的MVC架构、Active Record ORM、数据库配置、模型创建、数据库迁移、关联关系、查询优化以及性能提升策略。...