MongoDB
 Computer >> コンピューター >  >> プログラミング >> MongoDB

MongoDBの'count()'は非常に遅いです。どのようにそれを回避しますか?


sureIndex()を使用して、MongoDBのcount()メソッドのパフォーマンスを向上させることができます。概念を理解するために、ドキュメントを使用してコレクションを作成しましょう。ドキュメントを使用してコレクションを作成するためのクエリは次のとおりです-

> db.countPerformanceDemo.insertOne({"StudentName": "John"、 "StudentCountryName": "US"}); {"acknowledged":true、 "insertedId":ObjectId( "5c8ebcf82f684a30fbdfd55f")}>db。 countPerformanceDemo.insertOne({"StudentName": "Mike"、 "StudentCountryName": "UK"}); {"acknowledged":true、 "insertedId":ObjectId( "5c8ebd042f684a30fbdfd560")}> db.countPerformanceDemo.insertOne({" StudentName ":" David "、" StudentCountryName ":" AUS "}); {"acknowledged ":true、" insertedId ":ObjectId(" 5c8ebd112f684a30fbdfd561 ")}> db.countPerformanceDemo.insertOne({"StudentName ":" Carol " 、"StudentCountryName": "US"}); {"acknowledged":true、 "insertedId":ObjectId( "5c8ebd1a2f684a30fbdfd562")}> db.countPerformanceDemo.insertOne({"StudentName": "Bob"、 "StudentCountryName": " UK "}); {"acknowledged ":true、" insertedId ":ObjectId(" 5c8ebd212f684a30fbdfd563 ")}> db.countPerformanceDemo.insertOne({"StudentName ":" David "、" StudentCountryName ":" UK "}); { "acknowledged":true、 "insertedId":ObjectId( "5c8ebd9a2f684a30f bdfd564 ")}> db.countPerformanceDemo.insertOne({" StudentName ":" David "、" StudentCountryName ":" US "}); {"acknowledged ":true、" insertedId ":ObjectId(" 5c8ebd9e2f684a30fbdfd565 ")} 

find()メソッドを使用して、コレクションのすべてのドキュメントを表示します。クエリは次のとおりです-

> db.countPerformanceDemo.find()。pretty(); 

以下は出力です-

 {"_id":ObjectId( "5c8ebcf82f684a30fbdfd55f")、 "StudentName": "John"、 "StudentCountryName": "US"} {"_id":ObjectId( "5c8ebd042f684a30fbdfd560")、 "StudentName": "Mike" 、"StudentCountryName": "UK"} {"_id":ObjectId( "5c8ebd112f684a30fbdfd561")、 "StudentName": "David"、 "StudentCountryName": "AUS"} {"_id":ObjectId( "5c8ebd1a2f684a30fbdfd562")、 StudentName ":" Carol "、" StudentCountryName ":" US "} {" _id ":ObjectId(" 5c8ebd212f684a30fbdfd563 ")、" StudentName ":" Bob "、" StudentCountryName ":" UK "} {" _id ":ObjectId( "5c8ebd9a2f684a30fbdfd564")、 "StudentName": "David"、 "StudentCountryName": "UK"} {"_id":ObjectId( "5c8ebd9e2f684a30fbdfd565")、 "StudentName": "David"、 "StudentCountryName": " / pre> 

これは、count()の高性能形式を取得するためのクエリです-

> db.countPerformanceDemo.ensureIndex({"StudentName":1}); {"createdCollectionAutomatically":false、 "numIndexesBefore":1、 "numIndexesAfter":2、 "ok":1} 

ここで、count()メソッドを実装します。 StudentName“ David”のレコードをカウントします:

> db.countPerformanceDemo.find({"StudentName": "David"})。count(); 

以下は出力です-

 3 

  1. MongoDBで$pushを操作する

    ドキュメントを使用してコレクションを作成しましょう- > db.demo738.insertOne({Subjects:["C","C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead696557bb72a10bcf0661") } > db.demo738.insertOne({Subjects:["MySQL","PL/SQL

  2. MySQLCountDistinct値のプロセスは非常に遅いです。それを固定する方法は?

    プロセスを高速化するには、INDEXを使用できます。まずテーブルを作成しましょう- mysql> create table DemoTable1905    (    FirstName varchar(20),    LastName varchar(20) ,    INDEX F_L_Name(FirstName,LastName)    ); Query OK, 0 rows affected (0.00 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入しま