做自己看视频的网站,克拉玛依市建设局官方网站,口碑好网站建设公司,怎么在百度上面打广告在经常查询字段上创建索引。 在大数据的情况下#xff0c;在索引上查找可以提交10倍以上甚至1000倍的速度。 实际测试#xff0c;不在索引上查找用时12秒左右。建立索引#xff0c;在索引上查找速度提高只耗时1.1秒左右。当然索引也是一把双刃剑#xff0c;在一个表上创建索…在经常查询字段上创建索引。 在大数据的情况下在索引上查找可以提交10倍以上甚至1000倍的速度。 实际测试不在索引上查找用时12秒左右。建立索引在索引上查找速度提高只耗时1.1秒左右。当然索引也是一把双刃剑在一个表上创建索引过多反而降低查询速度。 注意 索引具有包含关系。 如// 创建复合唯一索引 WpEstateDataCompletenessSchema.index({ real_estate_name: 1, city: 1 }, { unique: true });实际创建的索引是这个形式real_estate_name_1_city_1。因为有这个索引实际上就不需要创建索引WpEstateDataCompletenessSchema.index({ real_estate_name: 1 });了当然你要是按照city查询需要创建索引WpEstateDataCompletenessSchema.index({ city: 1 }); 若遇到两条记录的主键冲突导致无法插入数据时重新创建符合唯一主键需要先删除原来的索引并且不能删除原来的主键后立即插入原来出问题的记录。要稍微等待下来避免缓存问题导致的删除索引失败问题。 下面是一个例子这个是原来以title作为主键又叫唯一索引
var Mongoose require(mongoose);
var ArticleUrlModel new Mongoose.Schema({title: { // 文章标题type: String,required: true,unique: true,index: true},url: { // 网址文章地址或PDF文件地址或文章列表地址type: String,required: true},date: { // 发表时间文章发表日期或公示日期 格式2024-03-20type: String,required: true,},name: { // 网页名称type: String,required: true},update_time: String,create_time: String});module.exports ArticleUrlModel;当时出现了一个异常不同网页的文章标题完全一样当插入了原来的文章标题记录后再插入一条相同的文章标题记录时报错误违反主键约束ceptionHandler { MongoError: E11000 duplicate key error collection:。 如何处理呢解决方案是在title:和name上创建唯一复合主键。删除原来的索引并注意缓存问题。 修改后的数据表结构
var Mongoose require(mongoose);
var ArticleUrlModel new Mongoose.Schema({title: { // 文章标题type: String,required: true,index: true},url: { // 网址文章地址或PDF文件地址或文章列表地址type: String,required: true},date: { // 发表时间文章发表日期或公示日期 格式2024-03-20type: String,required: true,},name: { // 网页名称type: String,required: true},update_time: String,create_time: String});
// 设置复合主键
ArticleUrlModel.index({ title: 1, name: 1 }, { unique: true });module.exports ArticleUrlModel;// 删除title字段的索引ArticleUrlModel.collection.dropIndex(title_1, function(err) {if (err) {console.log(Error dropping index:, err);} else {console.log(Index successfully dropped!);}});注意不能删除索引后立即插入原来冲突的记录。