'mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別'

MongoDB 數據庫 數據結構 架構師筆記 2019-08-18
"
"
mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

從頭開始,驗證mongodb的索引的好處。(window7環境下)

1 下載mongodb服務器,並解壓到d盤,並使用以下命令啟動

mongod --dbpath D:\\mongodb\\data

2 mongo客戶端Robo 3T 去官網下載,安裝

3 準備數據,條數為1億

public static void main(String[] args) { 
try {
/**** Connect to MongoDB ****/
// Since 2.10.0, uses MongoClient
MongoClient mongo = new MongoClient("localhost", 27017);
/**** Get database ****/
// if database doesn't exists, MongoDB will create it for you
DB db = mongo.getDB("www");
/**** Get collection / table from 'testdb' ****/
// if collection doesn't exists, MongoDB will create it for you
DBCollection table = db.getCollection("person");
/**** Insert ****/
// create a document to store key and value
BasicDBObject document=null;
for(int i=0;i<100000000;i++) {
document = new BasicDBObject();
document.put("name", "mkyong"+i);
document.put("age", 30);
document.put("sex", "f");
table.insert(document);
} /**** Done ****/
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}

4 獲取索引情況

"
mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

從頭開始,驗證mongodb的索引的好處。(window7環境下)

1 下載mongodb服務器,並解壓到d盤,並使用以下命令啟動

mongod --dbpath D:\\mongodb\\data

2 mongo客戶端Robo 3T 去官網下載,安裝

3 準備數據,條數為1億

public static void main(String[] args) { 
try {
/**** Connect to MongoDB ****/
// Since 2.10.0, uses MongoClient
MongoClient mongo = new MongoClient("localhost", 27017);
/**** Get database ****/
// if database doesn't exists, MongoDB will create it for you
DB db = mongo.getDB("www");
/**** Get collection / table from 'testdb' ****/
// if collection doesn't exists, MongoDB will create it for you
DBCollection table = db.getCollection("person");
/**** Insert ****/
// create a document to store key and value
BasicDBObject document=null;
for(int i=0;i<100000000;i++) {
document = new BasicDBObject();
document.put("name", "mkyong"+i);
document.put("age", 30);
document.put("sex", "f");
table.insert(document);
} /**** Done ****/
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}

4 獲取索引情況

mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

5 根據姓名查詢一條記錄

"
mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

從頭開始,驗證mongodb的索引的好處。(window7環境下)

1 下載mongodb服務器,並解壓到d盤,並使用以下命令啟動

mongod --dbpath D:\\mongodb\\data

2 mongo客戶端Robo 3T 去官網下載,安裝

3 準備數據,條數為1億

public static void main(String[] args) { 
try {
/**** Connect to MongoDB ****/
// Since 2.10.0, uses MongoClient
MongoClient mongo = new MongoClient("localhost", 27017);
/**** Get database ****/
// if database doesn't exists, MongoDB will create it for you
DB db = mongo.getDB("www");
/**** Get collection / table from 'testdb' ****/
// if collection doesn't exists, MongoDB will create it for you
DBCollection table = db.getCollection("person");
/**** Insert ****/
// create a document to store key and value
BasicDBObject document=null;
for(int i=0;i<100000000;i++) {
document = new BasicDBObject();
document.put("name", "mkyong"+i);
document.put("age", 30);
document.put("sex", "f");
table.insert(document);
} /**** Done ****/
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}

4 獲取索引情況

mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

5 根據姓名查詢一條記錄

mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

6 根據姓名創建索引

創建索引的時間稍微有點長,請耐心等待

db.person.createIndex({name:1})

"
mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

從頭開始,驗證mongodb的索引的好處。(window7環境下)

1 下載mongodb服務器,並解壓到d盤,並使用以下命令啟動

mongod --dbpath D:\\mongodb\\data

2 mongo客戶端Robo 3T 去官網下載,安裝

3 準備數據,條數為1億

public static void main(String[] args) { 
try {
/**** Connect to MongoDB ****/
// Since 2.10.0, uses MongoClient
MongoClient mongo = new MongoClient("localhost", 27017);
/**** Get database ****/
// if database doesn't exists, MongoDB will create it for you
DB db = mongo.getDB("www");
/**** Get collection / table from 'testdb' ****/
// if collection doesn't exists, MongoDB will create it for you
DBCollection table = db.getCollection("person");
/**** Insert ****/
// create a document to store key and value
BasicDBObject document=null;
for(int i=0;i<100000000;i++) {
document = new BasicDBObject();
document.put("name", "mkyong"+i);
document.put("age", 30);
document.put("sex", "f");
table.insert(document);
} /**** Done ****/
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}

4 獲取索引情況

mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

5 根據姓名查詢一條記錄

mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

6 根據姓名創建索引

創建索引的時間稍微有點長,請耐心等待

db.person.createIndex({name:1})

mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

7 索引情況

"
mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

從頭開始,驗證mongodb的索引的好處。(window7環境下)

1 下載mongodb服務器,並解壓到d盤,並使用以下命令啟動

mongod --dbpath D:\\mongodb\\data

2 mongo客戶端Robo 3T 去官網下載,安裝

3 準備數據,條數為1億

public static void main(String[] args) { 
try {
/**** Connect to MongoDB ****/
// Since 2.10.0, uses MongoClient
MongoClient mongo = new MongoClient("localhost", 27017);
/**** Get database ****/
// if database doesn't exists, MongoDB will create it for you
DB db = mongo.getDB("www");
/**** Get collection / table from 'testdb' ****/
// if collection doesn't exists, MongoDB will create it for you
DBCollection table = db.getCollection("person");
/**** Insert ****/
// create a document to store key and value
BasicDBObject document=null;
for(int i=0;i<100000000;i++) {
document = new BasicDBObject();
document.put("name", "mkyong"+i);
document.put("age", 30);
document.put("sex", "f");
table.insert(document);
} /**** Done ****/
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}

4 獲取索引情況

mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

5 根據姓名查詢一條記錄

mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

6 根據姓名創建索引

創建索引的時間稍微有點長,請耐心等待

db.person.createIndex({name:1})

mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

7 索引情況

mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

8 再一次查詢

"
mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

從頭開始,驗證mongodb的索引的好處。(window7環境下)

1 下載mongodb服務器,並解壓到d盤,並使用以下命令啟動

mongod --dbpath D:\\mongodb\\data

2 mongo客戶端Robo 3T 去官網下載,安裝

3 準備數據,條數為1億

public static void main(String[] args) { 
try {
/**** Connect to MongoDB ****/
// Since 2.10.0, uses MongoClient
MongoClient mongo = new MongoClient("localhost", 27017);
/**** Get database ****/
// if database doesn't exists, MongoDB will create it for you
DB db = mongo.getDB("www");
/**** Get collection / table from 'testdb' ****/
// if collection doesn't exists, MongoDB will create it for you
DBCollection table = db.getCollection("person");
/**** Insert ****/
// create a document to store key and value
BasicDBObject document=null;
for(int i=0;i<100000000;i++) {
document = new BasicDBObject();
document.put("name", "mkyong"+i);
document.put("age", 30);
document.put("sex", "f");
table.insert(document);
} /**** Done ****/
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}

4 獲取索引情況

mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

5 根據姓名查詢一條記錄

mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

6 根據姓名創建索引

創建索引的時間稍微有點長,請耐心等待

db.person.createIndex({name:1})

mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

7 索引情況

mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

8 再一次查詢

mongodb索引--1億條記錄的查詢從55.7秒到毫秒級別

索引說明:

索引通常能夠極大的提高查詢的效率,如果沒有索引,MongoDB在讀取數據時必須掃描集合中的每個文件並選取那些符合查詢條件的記錄。

這種掃描全集合的查詢效率是非常低的,特別在處理大量的數據時,查詢可以要花費幾十秒甚至幾分鐘,這對網站的性能是非常致命的。

索引是特殊的數據結構,索引存儲在一個易於遍歷讀取的數據集合中,索引是對數據庫表中一列或多列的值進行排序的一種結構。

http://www.runoob.com/mongodb/mongodb-indexing.html

createIndex() 方法

MongoDB使用 createIndex() 方法來創建索引。

注意在 3.0.0 版本前創建索引方法為 db.collection.ensureIndex(),之後的版本使用了 db.collection.createIndex() 方法,ensureIndex() 還能用,但只是 createIndex() 的別名。

語法

createIndex()方法基本語法格式如下所示:

>db.collection.createIndex(keys, options)

語法中 Key 值為你要創建的索引字段,1 為指定按升序創建索引,如果你想按降序來創建索引指定為 -1 即可。

實例

>db.col.createIndex({"title":1})
>

createIndex() 方法中你也可以設置使用多個字段創建索引(關係型數據庫中稱作複合索引)。

>db.col.createIndex({"title":1,"description":-1})
>

createIndex() 接收可選參數,可選參數列表如下:

ParameterTypeDescriptionbackgroundBoolean建索引過程會阻塞其它數據庫操作,background可指定以後臺方式創建索引,即增加 "background" 可選參數。 "background" 默認值為false。uniqueBoolean建立的索引是否唯一。指定為true創建唯一索引。默認值為false.namestring索引的名稱。如果未指定,MongoDB的通過連接索引的字段名和排序順序生成一個索引名稱。dropDupsBoolean3.0+版本已廢棄。在建立唯一索引時是否刪除重複記錄,指定 true 創建唯一索引。默認值為 false.sparseBoolean對文檔中不存在的字段數據不啟用索引;這個參數需要特別注意,如果設置為true的話,在索引字段中不會查詢出不包含對應字段的文檔.。默認值為 false.expireAfterSecondsinteger指定一個以秒為單位的數值,完成 TTL設定,設定集合的生存時間。vindex version索引的版本號。默認的索引版本取決於mongod創建索引時運行的版本。weightsdocument索引權重值,數值在 1 到 99,999 之間,表示該索引相對於其他索引字段的得分權重。default_languagestring對於文本索引,該參數決定了停用詞及詞幹和詞器的規則的列表。 默認為英語language_overridestring對於文本索引,該參數指定了包含在文檔中的字段名,語言覆蓋默認的language,默認值為 language.

實例

在後臺創建索引:

db.values.createIndex({open: 1, close: 1}, {background: true})

通過在創建索引時加 background:true 的選項,讓創建工作在後臺執行

"

相關推薦

推薦中...