subquery

Lazily evaluate MySQL view

▼魔方 西西 提交于 2019-12-25 09:16:29
问题 I have some MySQL views which define a number of extra columns based on some relatively straightforward subqueries. The database is also multi-tenanted so each row has a company ID against it. The problem I have is my views are evaluated for every row before being filtered by the company ID, giving huge performance issues. Is there any way to lazily evaluate the view so the 'where' clause in the outer query applies to the subqueries in the view. Or is there something similar to views that I

PostgreSQL: delete rows returned by subquery

∥☆過路亽.° 提交于 2019-12-25 09:00:21
问题 The basic syntax for DELETE is DELETE FROM table WHERE condition Is there a straightforward way to use subquery/alias in a DELETE statement, something like below? DELETE FROM (subquery) as sub WHERE condition Below is a minimal working table and my failed attempt to use subquery/alias: ---create table create table orderT ( id integer PRIMARY KEY, country_code varchar(2), created_date date, closed_date date); ---populate table INSERT INTO orderT VALUES (1, 'US', now(), now() + interval '1 day'

SQL: Retrieving total sum as subselect very slow

扶醉桌前 提交于 2019-12-25 08:17:05
问题 I am trying to fetch some averages and some sums over several rows, grouping by each hour of the day. Plus I want to fetch an additional column, where I don't get the sums for each hour (which is fetched when grouping), but where I want to fetch the total sum over all rows until that specific date. The SQL-statement is posted below. My problem is now, that executing the query on a MySQL database over ~25k rows takes about 8 seconds (CPU i5/8GB RAM). I identified that the subselect ( ... AS

WHERE … IN condition and multiple columns in subquery

喜你入骨 提交于 2019-12-25 07:59:23
问题 Is it please possible to rewrite the SQL query SELECT DISTINCT ON (uid) uid, female, given, photo, place FROM words_social WHERE uid IN (SELECT player1 FROM games) OR uid IN (SELECT player2 FROM games) ORDER BY uid, stamp DESC where first column player1 is fetched in a subquery and then column player2 is fetched from the same table? I've searched around and it seems that a JOIN should be used here instead of the 2 subqueries, but can not figure out exactly how. Just to give more context -

MySQL delete statement based on sub-select with multiple return values

◇◆丶佛笑我妖孽 提交于 2019-12-25 07:13:04
问题 MySQL delete statement based on sub-select with multiple return values. Here is what I am doing now: DELETE FROM `dnsstats` WHERE id NOT IN ( SELECT id FROM ( SELECT id FROM `dnsstats` WHERE peerhost = 'x.x.x.243' ORDER BY id DESC LIMIT 500 ) foo ) AND id NOT IN ( SELECT id FROM ( SELECT id FROM `dnsstats` WHERE peerhost = 'x.x.x.40' ORDER BY id DESC LIMIT 500 ) foo2 ) AND id NOT IN ( SELECT id FROM ( SELECT id FROM `dnsstats` WHERE peerhost = 'x.x.x.50' ORDER BY id DESC LIMIT 500 ) foo3 );

MySQL subquery to get a list of IDs from one table to query a second table

瘦欲@ 提交于 2019-12-25 06:41:08
问题 I have two tables, one is products and one salesRecords I am using this query: SELECT DAY(FROM_UNIXTIME(saleDate)) as day, MONTH(FROM_UNIXTIME(saleDate)) as mnth, YEAR(FROM_UNIXTIME(saleDate)) as yr, COUNT(id) as invCount, SUM(quantity) as qty FROM salesRecords WHERE itemNo IN (SELECT GROUP_CONCAT(convert(id, CHAR(8))) as ids FROM products WHERE brand =100 GROUP by brand) GROUP BY mnth, yr ORDER BY saleDate The products table contains all I need to know about a product, and salesRecords

mySQL order by count subquery trouble

浪尽此生 提交于 2019-12-25 05:22:59
问题 I have a table called items_status which has 3 fields, item_id, user_id, and status, which can be either 'have' or 'want'. Field Type Null Key user_id varchar(10) NO PRI item_id varchar(10) NO PRI status set('have','want') YES NULL I have a page where I want to get a list of all the user ids in the table ordered by the number of records their user id is associated with in the table where status is 'have'. So far, this is the best I can come up with: SELECT user_id FROM items_status AS is

doctrine subquery in select with join

前提是你 提交于 2019-12-25 05:15:17
问题 schema.yml: Membership: columns: id: { type: integer(4), notnull: true, unique: true, primary: true, autoincrement: true } organisation_id: { type: integer(4), notnull: true } membership_subcategory_id: { type: integer(4), notnull: true } start: { type: timestamp } end: { type: timestamp } amount: { type: decimal, size: 2 } amount_paid: { type: decimal, size: 2 } notes: { type: clob (16000000), notnull: false } payment_type: { type: string(20), notnull: true } order_id: { type: integer(4),

How to handle no rows returned in an Oracle update using a common sub-select

江枫思渺然 提交于 2019-12-25 04:51:57
问题 Consider the update: UPDATE table1 SET c1 = NVL(( SELECT d1 FROM table2 WHERE table1.id = table2.id ), 0), c2 = NVL(( SELECT d2 FROM table2 WHERE table1.id = table2.id ), 0) The NVL function handles the case where the sub-select returns no rows. Is there a good way to rewrite this (without repeating the sub-select) using this type of syntax: UPDATE table1 SET (c1,c2) = ( SELECT d1, d2 FROM table2 where table1.id = table2.id ) such that the case where the sub-select returns now rows is handled

How to handle no rows returned in an Oracle update using a common sub-select

浪子不回头ぞ 提交于 2019-12-25 04:51:19
问题 Consider the update: UPDATE table1 SET c1 = NVL(( SELECT d1 FROM table2 WHERE table1.id = table2.id ), 0), c2 = NVL(( SELECT d2 FROM table2 WHERE table1.id = table2.id ), 0) The NVL function handles the case where the sub-select returns no rows. Is there a good way to rewrite this (without repeating the sub-select) using this type of syntax: UPDATE table1 SET (c1,c2) = ( SELECT d1, d2 FROM table2 where table1.id = table2.id ) such that the case where the sub-select returns now rows is handled