对Model进行查询、保存、删除操作时速度很慢,从Debug中看到很多query [ 2.0 版本 ]
对Model进行查询、保存、删除操作时速度很慢,从Debug中看到很多query
SELECT
d.nspname AS table_schema,
c.relname AS table_name,
a.attname AS column_name,
COALESCE(td.typname, tb.typname, t.typname) AS data_type,
COALESCE(td.typtype, tb.typtype, t.typtype) AS type_type,
a.attlen AS character_maximum_length,
pg_catalog.col_description(c.oid, a.attnum) AS column_comment,
a.atttypmod AS modifier,
a.attnotnull = false AS is_nullable,
CAST(pg_get_expr(ad.adbin, ad.adrelid) AS varchar) AS column_default,
coalesce(pg_get_expr(ad.adbin, ad.adrelid) ~ 'nextval',false) AS is_autoinc,
CASE WHEN COALESCE(td.typtype, tb.typtype, t.typtype) = 'e'::char
THEN array_to_string((SELECT array_agg(enumlabel) FROM pg_enum WHERE enumtypid = COALESCE(td.oid, tb.oid, a.atttypid))::varchar[], ',')
ELSE NULL
END AS enum_values,
CASE atttypid
WHEN 21 /*int2*/ THEN 16
WHEN 23 /*int4*/ THEN 32
WHEN 20 /*int8*/ THEN 64
WHEN 1700 /*numeric*/ THEN
CASE WHEN atttypmod = -1
THEN null
ELSE ((atttypmod - 4) >> 16) & 65535
END
WHEN 700 /*float4*/ THEN 24 /*FLT_MANT_DIG*/
WHEN 701 /*float8*/ THEN 53 /*DBL_MANT_DIG*/
ELSE null
END AS numeric_precision,
CASE
WHEN atttypid IN (21, 23, 20) THEN 0
WHEN atttypid IN (1700) THEN
CASE
WHEN atttypmod = -1 THEN null
ELSE (atttypmod - 4) & 65535
END
ELSE null
END AS numeric_scale,
CAST(
information_schema._pg_char_max_length(information_schema._pg_truetypid(a, t), information_schema._pg_truetypmod(a, t))
AS numeric
) AS size,
a.attnum = any (ct.conkey) as is_pkey,
COALESCE(NULLIF(a.attndims, 0), NULLIF(t.typndims, 0), (t.typcategory='A')::int) AS dimension
FROM
pg_class c
LEFT JOIN pg_attribute a ON a.attrelid = c.oid
LEFT JOIN pg_attrdef ad ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
LEFT JOIN pg_type t ON a.atttypid = t.oid
LEFT JOIN pg_type tb ON (a.attndims > 0 OR t.typcategory='A') AND t.typelem > 0 AND t.typelem = tb.oid OR t.typbasetype > 0 AND t.typbasetype = tb.oid
LEFT JOIN pg_type td ON t.typndims > 0 AND t.typbasetype > 0 AND tb.typelem = td.oid
LEFT JOIN pg_namespace d ON d.oid = c.relnamespace
LEFT JOIN pg_constraint ct ON ct.conrelid = c.oid AND ct.contype = 'p'
WHERE
a.attnum > 0 AND t.typname != ''
AND c.relname = 'current_user'
AND d.nspname = 'public'
ORDER BY
a.attnum;
select
ct.conname as constraint_name,
a.attname as column_name,
fc.relname as foreign_table_name,
fns.nspname as foreign_table_schema,
fa.attname as foreign_column_name
from
(SELECT ct.conname, ct.conrelid, ct.confrelid, ct.conkey, ct.contype, ct.confkey, generate_subscripts(ct.conkey, 1) AS s
FROM pg_constraint ct
) AS ct
inner join pg_class c on c.oid=ct.conrelid
inner join pg_namespace ns on c.relnamespace=ns.oid
inner join pg_attribute a on a.attrelid=ct.conrelid and a.attnum = ct.conkey[ct.s]
left join pg_class fc on fc.oid=ct.confrelid
left join pg_namespace fns on fc.relnamespace=fns.oid
left join pg_attribute fa on fa.attrelid=ct.confrelid and fa.attnum = ct.confkey[ct.s]
where
ct.contype='f'
and c.relname='current_user'
and ns.nspname='public'
order by
fns.nspname, fc.relname, a.attnum
'enableSchemaCache' => true //这里设置为true或false都试过了
诸如此类的sql query,仅对2个postgresql数据库建立事务、设置事务隔离性,对3张数据表用实体(ActiveRecord)执行2-4条数据的插入、删除,这一过程耗费了4秒时间;当我不使用model操作时,用纯sql command命令去执行,这些sql query减少了三分之二,速度非常快。请问如何设置关闭这些query的出现呢?
Coder1024 补充于 2018-10-25 09:30
贴上部分代码
$datas = User::find()->where(['type' => $type])->with('user_type')->all();
if ($datas){
...
$transaction = User::getDb()->beginTransaction();
$transaction->setIsolationLevel(\yii\db\Transaction::SERIALIZABLE);
try {
$transaction2 = Customer::getDb()->beginTransaction();
$transaction2->setIsolationLevel(\yii\db\Transaction::SERIALIZABLE);
try {
foreach ($datas as $key => $user) {
...
$data = $data2 = $user->getAttributes();
...
$model = new UserInfo();
$model2 = new Customer();
$model->load($data, "");
$model2->load($data2, "");
if($model->save() && $model2->save() && $user->delete()){
$transaction2->commit();
$transaction->commit();
}
}
} catch(\Throwable $e) {
$transaction2->rollBack();
throw $e;
}
} catch(\Throwable $e) {
$transaction->rollBack();
throw $e;
}
}
最佳答案
其他 2 个回答
-
不知道 sqlcommand 执行和 AR 执行的代码怎么写的。你多贴一点代码瞅瞅。
共 4 条回复@Coder1024 Schema不删除缓存是不会重新加载的。应该还是代码要优化,sqlCommand 好点的话,就用sqlCommand
Coder1024
注册时间:2018-10-24
最后登录:2019-07-17
在线时长:2小时3分
最后登录:2019-07-17
在线时长:2小时3分
- 粉丝0
- 金钱45
- 威望0
- 积分65