저는 MongoDB를 처음 접했으며 관계형 데이터베이스 카운터 부분과 관련하여 NoSQL 데이터 모델의 쿼리 성능을 비교하고 싶었습니다. 나는 이것을 MongoDB 쉘에 썼다.
// Make 10 businesses
// Each business has 10 locations
// Each location has 10 departments
// Each department has 10 teams
// Each team has 100 employees
(new Array(10)).fill(0).forEach(_=>
db.businesses.insert({
"name":"Business Name",
"locations":(new Array(10)).fill(0).map(_=>({
"name":"Office Location",
"departments":(new Array(10)).fill(0).map(_=>({
"name":"Department",
"teams":(new Array(10)).fill(0).map(_=>({
"name":"Team Name",
"employees":(new Array(100)).fill(0).map(_=>({
"age":Math.floor(Math.random()*100)
}))
}))
}))
}))
})
);
그런 다음 다음 EXPLAIN SELECT age,name,(and a few other fields) FROM employees WHERE age >= 50 ORDER BY age DESC
문을 작성 하여 MySQL과 동등한 것을 시도했습니다 .
db.businesses.aggregate([
{ $unwind: "$locations" },
{ $unwind: "$locations.departments" },
{ $unwind: "$locations.departments.teams" },
{ $unwind: "$locations.departments.teams.employees" },
{ $project: { _id: 0, age: "$locations.departments.teams.employees.age" } },
{ $match: { "age": { $gte: 50 }} },
{ $sort: {"age" : -1}}
]).explain("executionStats")
결과는 다음과 같습니다.
"errmsg": "정렬이 104857600 바이트의 메모리 제한을 초과했지만 외부 정렬을 선택하지 않았습니다. 작업을 중단합니다. 선택하려면 allowDiskUse : true를 전달하십시오.",
그래서 나는 정렬 절을 삭제하고 explain
. 그러나 결과는 다음과 같습니다.
유형 오류 : db.businesses.aggregate (...). explain은 함수가 아닙니다.
그래서 내 질문은 다음과 같습니다.
주로 SELECT age FROM employees WHERE age >= 50 ORDER BY age DESC
MongoDB의 집계 쿼리 카운터 부분과 비교할 때 의 성능 차이를 알고 싶습니다 . 다소 동일합니까? 하나가 다른 것보다 훨씬 빠르거나 더 성능이 좋을까요?
또는 MySQL 쿼리 카운터 부분과 비교할 성능 세부 정보를 얻을 수 있도록 MongoDB 쿼리를 수정하려면 어떻게해야합니까?
직원은 단일 개체입니다. 따라서 age
부서, 위치 및 팀의 풍부한 구조에서 팀 구성원을 그렇게 깊이 모델링하고 싶지는 않을 것입니다 . 별도의 employees
컬렉션 을 갖고 간단하게 다음을 수행하는 것이 좋습니다.
db.businesses.aggregate([
{$match: {"age": {$gt: 50} }}
,{$sort: {"age": -1} }
]);
businesses
컬렉션 깊숙한 곳에서 다음을 가질 수 있습니다.
{ teams: [ {name: "T1", employees: [ "E1", "E34" ]} ] }
또는 다음을 시도하십시오.
db.businesses.aggregate([ your pipeline] ,{allowDiskUse:true});
OP는 10 개의 biz-> 10 loc-> 10 개의 depts-> 10 개의 팀-> 100 개의 emps로 설정되어 있습니다. 처음 3 번의 해제는 10000 배의 데이터 폭발을 생성하지만 마지막은 100 배 이상입니다. 다음을 사용하여 히트를 줄일 수 있습니다 $filter
.
db.businesses.aggregate([
{ $unwind: "$locations" },
{ $unwind: "$locations.departments" },
{ $unwind: "$locations.departments.teams" },
{$project: {
XX: {$filter: {
input: "$locations.departments.teams.employees",
as: "z",
cond: {$gte: [ "$$z.age", 50] }
}}
}}
,{$unwind: "$XX"}
,{$sort: {"XX.age":-1}}])
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다