简体   繁体   中英

How i can define index for a table in node.js (adonis)

Actually i have this table:

ClassBookHistoric
-----------------
Id (Primary Key), 
Class_Id (Unique key, foreign key of table Class),
BookUnit_Id (Unique key, foreign key of table BookUnits) 
Book_Id 
Status

I need to make queries to search data like this: SELECT WHERE Class_Id = (parameter), Book_Id = (parameter) and Status = 1 (active)

I'm studying about indexes and i'm thinking it is necessary to create a index using the columns that i will use to search data ( Class_Id, Book_Id and Status ) to increase the performance. There's a way to create a index to a group of columns: ( Class_Id, Book_Id, Status )? If it's possible, how i can create a group of index in node.js/adonis?

Adonis.js uses knex.js to define columns types and other modifiers as you can see from thedocs .

So as an example based on your schema (not fully working just to demonstrate):

'use strict'

const Schema = use('Schema')

class ClassBookHistoric extends Schema {
  up () {
    this.create('class_books', (table) => {
      table.increments()
      table.integer('class_id').notNullable().unique()
      table.integer('book_unit_Id').notNullable().unique()
      table.index(['class_id','book_unit_Id'], 'class_book_index');
      table.timestamps()
    })
  }

  down () {
    this.drop('class_books')
  }
}

module.exports = UsersSchema

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM