我正在执行下面的查询,几秒钟后我得到一个" MySQL服务器已经消失了#34;错误。我试图增加wait_timeout,max_allowed_packet以及人们建议的一些其他变量,但我无法修复它。
class Service extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
public function company () {
return $this->belongsTo('Company');
}
}
class Company extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
public function services() {
return $this->hasMany('Service');
}
}
Schema::create('services', function (Blueprint $table) {
$table->increments('id');
$table->integer('company_id');
$table->integer('service_category_id');
$table->integer('server_id');
$table->string('title');
$table->string('description');
$table->string('icon');
$table->boolean('accepts_swaps');
$table->integer('qty_available');
$table->double('price_usd', 10, 6);
$table->timestamps();
$table->softDeletes();
});
Schema::create('companies', function (Blueprint $table) {
$table->increments('id');
$table->integer('owner_id');
$table->string('name');
$table->string('email');
$table->string('paypal_email')->nullable();
$table->string('skrill_email')->nullable();
$table->string('contact_email')->nullable();
$table->string('phone')->nullable();
$table->integer('city_id')->nullable();
$table->string('short_description')->nullable();
$table->text('description')->nullable();
$table->integer('subscription_id')->nullable();
$table->timestamp('subscription_end_date')->nullable();
$table->string('avatar')->default("img/default/user-avatar-128.min.png");
$table->integer('highlighted_game_id')->nullable()->default(null);
$table->timestamps();
$table->softDeletes();
});
^这是导致我从日志文件中获得麻烦的查询。
如果我更改" SELECT tmp.filter_group_id,tmp.filter_id,tmp.product_id"部分用" SELECT tmp。*"查询工作正常,并在0.2秒内返回结果。不幸地改变它不会解决我的问题,因为还有另一个查询,例如:
SELECT tmp.filter_group_id,
tmp.filter_id,
tmp.product_id
FROM
(SELECT *
FROM
(SELECT f.filter_group_id,
pf.filter_id,
p.product_id,
(IFNULL(
(SELECT price
FROM oc_product_special AS ps
WHERE ps.product_id = p.product_id
AND ps.customer_group_id = '1'
AND ((ps.date_start = '0000-00-00'
OR ps.date_start < NOW())
AND (ps.date_end = '0000-00-00'
OR ps.date_end > NOW()))
ORDER BY ps.priority ASC, ps.price ASC LIMIT 1),
IFNULL(
(SELECT price
FROM oc_product_discount AS pd2
WHERE pd2.product_id = p.product_id
AND pd2.customer_group_id = '1'
AND pd2.quantity >= '1'
AND ((pd2.date_start = '0000-00-00'
OR pd2.date_start < NOW())
AND (pd2.date_end = '0000-00-00'
OR pd2.date_end > NOW()))
ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1), p.price)) * 1) AS mf_price
FROM oc_product AS p
INNER JOIN oc_product_filter AS pf ON pf.product_id = p.product_id
INNER JOIN oc_filter AS f ON f.filter_id = pf.filter_id
INNER JOIN oc_product_to_store AS p2s ON p2s.product_id = p.product_id
AND p2s.store_id = 0
INNER JOIN oc_product_to_category AS p2c ON p2c.product_id = p.product_id
INNER JOIN oc_category_path AS cp ON cp.category_id = p2c.category_id
WHERE p.date_available <= NOW()
AND p.status = '1'
AND (FIND_IN_SET(18, p.mfilter_values))
AND cp.path_id IN(6) ) AS tmp
WHERE (mf_price > 40
AND mf_price < 701) ) AS tmp
WHERE tmp.filter_group_id NOT IN(1);
所以我必须得到第一个查询。有什么建议吗?
编辑:
这是mysql.log
SELECT mfp.filter_group_id,
mfp.filter_id,
COUNT(DISTINCT mfp.product_id) FROM ([THE QUERY ABOVE]) as mfp
GROUP BY mfp.filter_group_id,
mfp.filter_id;
编辑2: 这是MySQL 5.7.9服务器,它在我的Windows机器上运行,我正在使用WAMP。
我的共享主机服务器上有两个相同的数据库(行数至少增加了5倍):
几乎立即运行查询。
答案 0 :(得分:1)
此问题似乎与Bug #79787具有相同的症状。虽然没有明确记录,但这似乎已在MySQL Server 5.7.10中得到解决,该实际上是在报告错误前几天发布的。
在派生表的选择列表中使用嵌套派生表和标量子查询的查询在某些情况下可能会导致服务器退出。 (Bug#22062023)
https://dev.mysql.com/doc/relnotes/mysql/5.7/en/news-5-7-10.html#mysqld-5-7-10-bug
“服务器退出”是“一次严重崩溃”的委婉说法。
这似乎是由defect错误地认为列“未使用”并将其优化掉,最终导致无效的内存访问,因为该列实际上是在外部查询中使用
升级到5.7.10或更高版本可以解决可能在5.7开发早期引入的问题,因此可能在5.7.0之前根本不存在。
答案 1 :(得分:0)
将此'复合'索引添加到ps和pd2:
INDEX(customer_group_id, priority, price)
如果仍然失败,请在http://bugs.mysql.com写一个错误报告; MySQL不应该崩溃。