-
-
Save ualmtorres/2f99fff89517687bcd87aaf8523fd112 to your computer and use it in GitHub Desktop.
Script MongoDB Indexación
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Consulta de índices disponibles | |
*/ | |
use test | |
db.scores.getIndexes() | |
/* | |
* Lento porque no esta indexado y hay que recorrerlo todo | |
*/ | |
db.scores.find({'student_id': 1}); | |
/* | |
* Rapido porque lo encuentra rapido al hacer el recorrido secuencial | |
*/ | |
db.scores.findOne({'student_id': 1}); | |
/* | |
* Lento porque tarda en encontrarlo al hacer el recorrido secuencial | |
*/ | |
db.zips.findOne({'student_id': 1000000}); | |
/* | |
* Lento porque no esta indexado y tiene que recorrerlo todo | |
*/ | |
db.zips.find({'student_id': 1000000}); | |
/* | |
* Creación de índices | |
*/ | |
db.scores.getIndexes() | |
db.scores.find({student_id:999}).explain('executionStats') | |
db.scores.createIndex({student_id:1}) | |
db.scores.find({student_id:999}).explain('executionStats') | |
db.scores.createIndex({student_id:1, type:1}) | |
db.scores.find({type:'examen'}).explain('executionStats') | |
db.scores.getIndexes() | |
/* | |
* Eliminación de índices | |
*/ | |
db.scores.dropIndex({student_id:1}) | |
db.scores.dropIndex({student_id:1, type:1}) | |
db.scores.getIndexes() | |
/* | |
* Indices compuestos y arrays | |
*/ | |
db.multikey.createIndex({a:1, b:1}) | |
db.multikey.insertOne({a:1, b:1}) | |
db.multikey.insertOne({a:[1, 2, 3], b:1}) | |
db.multikey.insertOne({a:1, b:[1, 2, 3]}) | |
db.multikey.insertOne({a:[1, 2, 3], b:[1, 2, 3]}) | |
/* | |
* Indices definidos a cualquier nivel de documento | |
*/ | |
db.people.createIndex({'addresses.tag':1}); | |
db.people.createIndex({'addresses.phone':1}); | |
db.people.getIndexes(); | |
/* | |
* Indices sin duplicados | |
*/ | |
db.stuff.insertOne({cosa: 'ordenador'}) | |
db.stuff.insertOne({cosa: 'puerta'}) | |
db.stuff.createIndex({cosa: 1}, {unique:true}) | |
db.stuff.getIndexes() | |
/* | |
* Creación de índices con eliminación de duplicados | |
*/ | |
db.stuff.drop() | |
db.stuff.insertOne({cosa: 'ordenador', marca: 'hp'}) | |
db.stuff.insertOne({cosa: 'puerta', marca: 'acme'}) | |
db.stuff.createIndex({cosa: 1}, {unique:true}) | |
db.stuff.insertOne({cosa: 'ordenador', marca: 'toshiba'}) | |
db.stuff.find() | |
/* | |
* Indices dispersos (Admiten valores nulos) | |
*/ | |
db.stuff.insertOne({cosa: 'coche', color:'azul'}) | |
db.stuff.insertOne({cosa: 'moto', color:'verde'}) | |
db.stuff.createIndex({color: 1}, {unique: true, sparse: true}) | |
/* | |
* Documentos sin el atributo color no son recuperados al usar el indice disperso de color | |
*/ | |
db.stuff.find({}, {_id: 0}) | |
db.stuff.find({}, {_id: 0}).sort({color: 1}).hint({color: 1}) | |
/* | |
* Uso de índices por una consulta | |
*/ | |
db.foo.createIndex({a:1, b:1, c:1}) | |
// Consultas que usarán el índice | |
db.foo.find({a:3}).explain('executionStats') | |
db.foo.find({c:1}).sort({a:1, b:1}).explain('executionStats') | |
// Consultas que no usarán el índice | |
db.foo.find({b:3, c:4}).explain('executionStats') | |
db.foo.find({c:1}).sort({a:-1, b:1}).explain('executionStats') | |
/* | |
* Indices geoespaciales | |
*/ | |
use test; | |
db.places.insertOne({name: 'Juani', type: 'Peluquería', location: [40, 41.22]}); | |
db.places.insertOne({name: 'Andrea', type: 'Peluquería', location: [39.18, 40.12]}); | |
db.places.insertOne({name: '3 llaves', type: 'Menaje', location: [40.232, -74.343]}); | |
db.places.insertOne({name: 'Supertodo', type: 'Alimentación', location: [41.232, -75.343]}); | |
// Creación del índice 2D | |
db.places.createIndex({location: '2d', type: 1}); | |
// Documentos de peluquerías más cercanas a [40, 40] | |
db.places.find({type: 'Peluquería', location:{$near: [40, 40]}}, {_id:0}); | |
//Documento de peluquería más cercana a [40, 40] | |
db.places.find({type: 'Peluquería', location:{$near:[40, 40]}}, {_id:0}).limit(1) | |
/* | |
* Registro de consultas lentas. El profiler | |
*/ | |
// Consulta del estado del Profiler | |
db.getProfilingStatus() | |
// Activar el Profiler para registrar consultas que tarden más de 4 ms | |
db.setProfilingLevel(1, 4) | |
// Consulta del estado del Profiler | |
db.getProfilingStatus() | |
// Desactivación del Profiler | |
db.setProfilingLevel(0) | |
// Consulta del estado del Profiler | |
db.getProfilingStatus() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment