【发布时间】:2016-10-09 10:29:40
【问题描述】:
作为一个 MySql 业余爱好者,我想请教一些关于表优化和索引使用的建议。
考虑一个包含用户发布的广告的表格。该表具有以下结构(这是一个 Laravel 实现,但我认为代码非常不言自明):
Schema::create('advertisements', function (Blueprint $table) {
$table->increments('id'); //PRIMARY KEY AUTOINCREMENTS
$table->text('images'); //TEXT
$table->string('name', 150); //VARCHAR(150)
$table->string('slug'); //VARCHAR(255)
$table->text('description');
$table->string('offer_type',7)->nullable()->index();
$table->float('price')->nullable();
$table->string('deal_type')->nullable()->index();
$table->char('price_period',1)->nullable()->index();
$table->float('price_per_day')->nullable();
$table->float('deposit')->nullable();
$table->integer('category_id')->unsigned()->index();
$table->foreign('category_id')->references('id')->on('categories');
$table->integer('author_id')->unsigned()->nullable();
$table->foreign('author_id')->references('id')->on('users');
$table->timestamps();
});
网站上的用户可以使用多个条件搜索上表中的广告,例如:price range、offer_type、price_period 或 deal_type。
如您所见,我已对 offer_type、price_period 和 deal_type 列进行了索引。据我了解,这会导致数据库为这些列中的值创建 BTREE 索引。
但是,这些值总是来自预定义的集合:
例如 - price_period 始终是以下之一:NULL, h, d, w, m, y(小时、日、周、月、年。)
deal_type 列始终是 offer 或 demand。
问题: 如果我有一组列只包含来自预定义的小范围值的值,那么创建一个单独的值是否更好(性能方面)表并使用外键而不是索引列? 编辑:经过进一步研究,我现在意识到,外键只是一种参考工具,而不是一种性能工具,它们也可以(并且应该)被索引.但是索引外键(一个数字)是否比索引短字符串的性能更好?
【问题讨论】:
标签: mysql indexing database-indexes