SELECT column_name as name,
data_type,
concat(character_maximum_length,numeric_precision,datetime_precision) as length,
concat(numeric_precision,datetime_precision) as prec,
is_nullable as isnullable,
COALESCE(numeric_scale,'0') as scale,
null as definition,
null as iscomputed,
null as is_sparse,
column_default as "default",
null as is_persisted
FROM information_schema.columns
WHERE (table_schema, table_name) = ('test', 'testtable')
ORDER BY ordinal_position;
Current output:
name | data_type | length | prec | isnullable | scale | definition | iscomputed | is_sparse | default | is_persisted
----------------------+-----------------------------+--------+------+------------+-------+------------+------------+-----------+--------------------+--------------
keytestpostgresql | uuid | | | NO | 0 | | | | uuid_generate_v4() |
testsmalldatetime | timestamp without time zone | 0 | 0 | YES | 0 | | | | |
testdatetime | timestamp without time zone | 3 | 3 | YES | 0 | | | | |
testint | integer | 32 | 32 | YES | 0 | | | | |
testdatenotime | date | 0 | 0 | YES | 0 | | | | |
testnmemo | text | | | YES | 0 | | | | |
testfloat | double precision | 53 | 53 | YES | 0 | | | | |
testboolean | boolean | | | YES | 0 | | | | |
biginttest | bigint | 64 | 64 | YES | 0 | | | | |
testtinyint | smallint | 16 | 16 | YES | 0 | | | | |
testsmallint | smallint | 16 | 16 | YES | 0 | | | | |
testuniqueidentifier | uuid | | | YES | 0 | | | | |
testnumeric | numeric | 5 | 5 | YES | 2 | | | | |
updoperation | smallint | 16 | 16 | NO | 0 | | | | 0 |
upddate | timestamp without time zone | 3 | 3 | NO | 0 | | | | now() |
(15 rows)
Want column length to return or show value in output as -1 if blank or null
Want column prec to return or show value in output as 0 if blank or null
Please refer below Query:
SELECT column_name as name,
data_type,
coalesce(character_maximum_length, numeric_precision, datetime_precision, -1) as length,
concat(numeric_precision,datetime_precision) as prec,
is_nullable as isnullable,
COALESCE(numeric_scale,'0') as scale,
null as definition,
null as iscomputed,
null as is_sparse,
column_default as "default",
null as is_persisted
FROM information_schema.columns
WHERE (table_schema, table_name) = ('test', 'testtable')
ORDER BY ordinal_position;
Related
This question already has answers here:
ERROR 1054 (42S22): Unknown column '' in 'field list'
(2 answers)
Closed 3 years ago.
mysql> desc apn_licence;
+------------+------------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+-------------------+-------+
| vdomain | char(32) | NO | PRI | test | |
| vhost | char(32) | NO | PRI | | |
| licence | char(64) | NO | | | |
| tcpport | int(11) | NO | | 0 | |
| srvtype | tinyint(4) | NO | | 0 | |
| auxcmd | tinyint(3) | YES | | 0 | |
| agentuid | int(10) unsigned | NO | | 1 | |
| owneruid | int(10) unsigned | NO | | 1 | |
| sno | char(32) | YES | | 0 | |
| pno | char(32) | YES | | 0 | |
| updatedate | timestamp | NO | | CURRENT_TIMESTAMP | |
| builddate | timestamp | NO | | CURRENT_TIMESTAMP | |
+------------+------------------+------+-----+-------------------+-------+
12 rows in set (0.00 sec)
mysql> desc apn_agent;
+--------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------------+------+-----+---------+----------------+
| uid | int(10) unsigned | NO | PRI | NULL | auto_increment |
| euid | int(10) unsigned | YES | | 0 | |
| username | char(16) | NO | UNI | | |
| password | char(16) | YES | | NULL | |
| priv_licence | tinyint(3) unsigned | NO | | 0 | |
| priv_admin | tinyint(3) unsigned | NO | | 0 | |
| priv_srv | tinyint(3) unsigned | NO | | 0 | |
| plant_asign | int(11) | YES | | 0 | |
+--------------+---------------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
mysql> desc apn_routine;
+------------+----------------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------------------+------+-----+-------------------+-------+
| acxid | int(10) unsigned | NO | PRI | 0 | |
| sid | int(10) unsigned | NO | PRI | 0 | |
| wanipaddr | int(10) unsigned | YES | | 0 | |
| udpport | smallint(5) unsigned | NO | | 8400 | |
| serial | bigint(20) unsigned | YES | | 0 | |
| nserial | bigint(20) unsigned | YES | | 0 | |
| rivec | char(32) | YES | | NULL | |
| stat | smallint(5) unsigned | YES | | 4 | |
| vtype | smallint(5) unsigned | YES | | 0 | |
| gwtype | smallint(5) unsigned | YES | | 2 | |
| vactlevel | int(10) unsigned | YES | | 0 | |
| vnatlevel | int(10) unsigned | YES | | 0 | |
| vdomain | char(32) | NO | | | |
| vhost | char(32) | NO | | | |
| vsubkey | char(32) | YES | | public | |
| passwd | char(64) | YES | | NULL | |
| gwmac | char(18) | YES | MUL | 00:0D:B0:00:00:00 | |
| vsubnet | int(10) unsigned | YES | | 0 | |
| vsubmask | int(10) unsigned | YES | | 0 | |
| vgwip | int(10) unsigned | YES | | 0 | |
| vgwnexthop | int(10) unsigned | YES | | 0 | |
| groupstamp | timestamp | NO | | CURRENT_TIMESTAMP | |
| joinstamp | timestamp | NO | | CURRENT_TIMESTAMP | |
+------------+----------------------+------+-----+-------------------+-------+
23 rows in set (0.00 sec)
With mysql-3.23.58
select l.*, a.username from apn_licence as l, apn_agent as a
left join apn_routine as r on l.vdomain=r.vdomain and l.vhost=r.vhost
where
l.agentuid=a.uid
group by l.vdomain
order by l.tcpport DESC,l.vdomain,l.vhost;
is OK.
But with mysql-5.7.26,it get the error as:
ERROR 1054 (42S22): Unknown column 'l.vdomain' in 'on clause'
Looks like there are invisible garbage characters in your query.
Try retyping the query (don't copy and paste or you'll most likely include the garbage character) and most probably it should work.
Thanks all,I found the answer in
[MySQL unknown column in ON clause
The correct query is
select l.*, a.username from (apn_licence as l, apn_agent as a)
left join apn_routine as r on l.vdomain=r.vdomain and l.vhost=r.vhost
where
l.agentuid=a.uid
group by l.vdomain
order by l.tcpport DESC,l.vdomain,l.vhost;
This question already has answers here:
Cannot simply use PostgreSQL table name ("relation does not exist")
(18 answers)
I keep getting the error "relation [TABLE] does not exist"
(1 answer)
PostgreSQL "Column does not exist" but it actually does
(6 answers)
Closed 4 years ago.
I am facing problem similar to this question,
I have restored this database from a dump.
Here is my problem,
test_api=# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+-------------------------------+----------+---------------+------------+-------------
public | vehiclenumber | table | luvpreetsingh | 8192 bytes |
public | vehiclenumber_id_seq | sequence | luvpreetsingh | 8192 bytes |
public | launchPad_pair | table | luvpreetsingh | 8192 bytes |
All tables are in public schema(I have posted only relevant tables here). I am able to query from vehiclenumber table.
test_api=# select * from iot_vehiclenumber;
id | vn | rk | rt | seed | vt
----+------+-----+-------+------+----
1 | 4513 | NO | RESET | 1234 | 01
2 | 1234 | YES | RESET | 1234 | 01
(2 rows)
But I am not able to query from launchPad_pair table.
test_api=# select * from launchPad_pair;
ERROR: relation "launchpad_pair" does not exist
LINE 1: select * from launchPad_pair;
test_api=# select * from public.launchPad_pair;
ERROR: relation "public.launchpad_pair" does not exist
LINE 1: select * from public.launchPad_pair;
I run the following query and it spits out all the info(again to make sure the schema is public),
test_api=# SELECT * FROM information_schema.columns where table_name='launchPad_pair';
table_catalog | table_schema | table_name | column_name | ordinal_position | column_default | is_nullable | data_type | character_maximum_length | character_octet_length | numeric_precision | numeric_precision_radix | numeric_scale | datetime_precision | interval_type | interval_precision | character_set_catalog | character_set_schema | character_set_name | collation_catalog | collation_schema | collation_name | domain_catalog | domain_schema | domain_name | udt_catalog | udt_schema | udt_name | scope_catalog | scope_schema | scope_name | maximum_cardinality | dtd_identifier | is_self_referencing | is_identity | identity_generation | identity_start | identity_increment | identity_maximum | identity_minimum | identity_cycle | is_generated | generation_expression | is_updatable
---------------+--------------+----------------+-------------+------------------+-----------------------------------------------+-------------+-----------+--------------------------+------------------------+-------------------+-------------------------+---------------+--------------------+---------------+--------------------+-----------------------+----------------------+--------------------+-------------------+------------------+----------------+----------------+---------------+-------------+--------------+------------+----------+---------------+--------------+------------+---------------------+----------------+---------------------+-------------+---------------------+----------------+--------------------+------------------+------------------+----------------+--------------+-----------------------+--------------
test_api | public | launchPad_pair | id | 1 | nextval('"launchPad_pairs_id_seq"'::regclass) | NO | integer | | | 32 | 2 | 0 | | | | | | | | | | | | | test_api | pg_catalog | int4 | | | | | 1 | NO | NO | | | | | | NO | NEVER | | YES
test_api | public | launchPad_pair | vehicle_id | 2 | | NO | integer | | | 32 | 2 | 0 | | | | | | | | | | | | | test_api | pg_catalog | int4 | | | | | 2 | NO | NO | | | | | | NO | NEVER | | YES
test_api | public | launchPad_pair | Box_id | 3 | | NO | integer | | | 32 | 2 | 0 | | | | | | | | | | | | | test_api | pg_catalog | int4 | | | | | 3 | NO | NO | | | | | | NO | NEVER | | YES
I have checked many times to make sure there is no typo.
What is the problem here?
I created a table with 10GB (60M records), added unique index (hidden_id) manually after data insertion.
I had the simplest query but it took one minutes to complete.
select hidden_id from netflow where hidden_id = 350000;
And also the query took tens of minutes "select * from netflow order by hidden_id limit 12500 offset 212500;".
It really confuses me.
I post the analysis of the first query below. Any clue why it's so slow?
trace select hidden_id from netflow where hidden_id = 350000;
+----------+------------------------------------------------------------------+
| ticks | stmt |
+==========+==================================================================+
| 3 | X_3 := sql.mvc(); |
| 15 | X_7=<tmp_2510>[69396995] := sql.bind(X_3=0,"sys","netflow","hidd |
: : en_id",0); :
| 227 | X_4:bat[:oid,:oid] =<tmp_13332>[69396995] := sql.tid(X_3=0,"sys" |
: : ,"netflow"); :
| 72978741 | X_36=<tmp_4053>[1] := algebra.subselect(X_7=<tmp_2510>[69396995] |
: : ,X_4=<tmp_13332>:bat[:oid,:oid][69396995],A0=350000:lng,A0=35000 :
: : 0:lng,true,true,false); :
| 17 | (X_10=<tmp_2175>[0],r1_10=<tmp_3416>[0]) := sql.bind(X_3=0,"sys" |
: : ,"netflow","hidden_id",2); :
| 14 | X_37=<tmp_13337>[0] := algebra.subselect(r1_10=<tmp_3416>[0],A0= |
: : 350000:lng,A0=350000:lng,true,true,false); :
| 6 | X_13=<tmp_3416>[0] := sql.bind(X_3=0,"sys","netflow","hidden_id" |
: : ,1); :
| 15 | X_38=<tmp_11053>[0] := algebra.subselect(X_13=<tmp_3416>[0],X_4= |
: : <tmp_13332>:bat[:oid,:oid][69396995],A0=350000:lng,A0=350000:lng :
: : ,true,true,false); :
| 4 | X_15=<tmp_4053>[1] := sql.subdelta(X_36=<tmp_4053>[1],X_4=<tmp_1 |
: : 3332>:bat[:oid,:oid][69396995],X_10=<tmp_2175>[0],X_37=<tmp_1333 :
: : 7>[0],X_38=<tmp_11053>[0]); :
| 20 | X_17=<tmp_11053>[1] := sql.projectdelta(X_15=<tmp_4053>[1],X_7=< |
: : tmp_2510>[69396995],X_10=<tmp_2175>[0],r1_10=<tmp_3416>[0],X_13= :
: : <tmp_3416>[0]); :
| 6 | X_18 := sql.resultSet(1,1,X_17=<tmp_11053>[1]); |
| 7 | sql.rsColumn(X_18=1,"sys.netflow","hidden_id","bigint",64,0,X_17 |
: : =<tmp_11053>[1]); :
| 2 | X_23 := io.stdout(); |
| 25 | sql.exportResult(X_23=="104d2":streams,X_18=1); |
| 1 | end s1_3; |
| 73011629 | X_5:void := user.s1_3(350000:lng); |
+----------+------------------------------------------------------------------+
This is the table being created.
CREATE TABLE "netflow" (
"time_seconds" double DEFAULT NULL,
"parsed_date" timestamp DEFAULT NULL,
"date_time_str" varchar(45) DEFAULT NULL,
"ip_layer_protocol" bigint DEFAULT NULL,
"ip_layer_protocol_code" varchar(45) DEFAULT NULL,
"first_seen_src_ip" varchar(45) DEFAULT NULL,
"first_seen_dest_ip" varchar(45) DEFAULT NULL,
"first_seen_src_port" bigint DEFAULT NULL,
"first_seen_dest_port" bigint DEFAULT NULL,
"more_fragments" varchar(45) DEFAULT NULL,
"cont_fragments" varchar(45) DEFAULT NULL,
"duration_seconds" bigint DEFAULT NULL,
"first_seen_src_payload_bytes" bigint DEFAULT NULL,
"first_seen_dest_payload_bytes" bigint DEFAULT NULL,
"first_seen_src_total_bytes" bigint DEFAULT NULL,
"first_seen_dest_total_bytes" bigint DEFAULT NULL,
"first_seen_src_packet_count" bigint DEFAULT NULL,
"first_seen_dest_packet_count" bigint DEFAULT NULL,
"record_force_out" varchar(45) DEFAULT NULL
);
Update:
the platform: Windows 7 without parallel
MonetDB version: MonetDB 5 server v11.15.19 "Feb2013-SP6"
description of the table in storage:
select * from storage() where "table"='netflow';
+--------+---------+-------------------------------+-----------+----------+-----
-----+-----------+------------+------------+---------+--------+
| schema | table | column | type | location | count | typewidth | columnsize | heapsize | indices | sorted |
+========+=========+===============================+===========+==========+==========+===========+============+============+=========+========+
| sys | netflow | time_seconds | double | 17\1711 | 69396995 | 8 | 555175960 | 0 | 0 | false |
| sys | netflow | parsed_date | timestamp | 20\2054 | 69396995 | 8 | 555175960 | 0 | 0 | false |
| sys | netflow | date_time_str | varchar | 07\734 | 69396995 | 21 | 277587980 | 2684354560 | 0 | false |
| sys | netflow | ip_layer_protocol | bigint | 62\6261 | 69396995 | 8 | 555175960 | 0 | 0 | false |
| sys | netflow | ip_layer_protocol_code | varchar | 62\6213 | 69396995 | 3 | 69396995 | 524288 | 0 | false |
| sys | netflow | first_seen_src_ip | varchar | 63\6342 | 69396995 | 11 | 138793990 | 524288 | 0 | false |
| sys | netflow | first_seen_dest_ip | varchar | 23\2324 | 69396995 | 8 | 138793990 | 524288 | 0 | false |
| sys | netflow | first_seen_src_port | bigint | 15\1574 | 69396995 | 8 | 555175960 | 0 | 0 | false |
| sys | netflow | first_seen_dest_port | bigint | 23\2370 | 69396995 | 8 | 555175960 | 0 | 0 | false |
| sys | netflow | more_fragments | varchar | 65\6521 | 69396995 | 1 | 69396995 | 524288 | 0 | false |
| sys | netflow | cont_fragments | varchar | 65\6524 | 69396995 | 1 | 69396995 | 524288 | 0 | false |
| sys | netflow | duration_seconds | bigint | 65\6560 | 69396995 | 8 | 555175960 | 0 | 0 | false |
| sys | netflow | first_seen_src_payload_bytes | bigint | 65\6561 | 69396995 | 8 | 555175960 | 0 | 0 | false |
| sys | netflow | first_seen_dest_payload_bytes | bigint | 65\6562 | 69396995 | 8 | 555175960 | 0 | 0 | false |
| sys | netflow | first_seen_src_total_bytes | bigint | 65\6563 | 69396995 | 8 | 555175960 | 0 | 0 | false |
| sys | netflow | first_seen_dest_total_bytes | bigint | 65\6564 | 69396995 | 8 | 555175960 | 0 | 0 | false |
| sys | netflow | first_seen_src_packet_count | bigint | 65\6565 | 69396995 | 8 | 555175960 | 0 | 0 | false |
| sys | netflow | first_seen_dest_packet_count | bigint | 65\6566 | 69396995 | 8 | 555175960 | 0 | 0 | false |
| sys | netflow | record_force_out | varchar | 65\6567 | 69396995 | 1 | 69396995 | 524288 | 0 | false |
| sys | netflow | hidden_id | bigint | 25\2510 | 69396995 | 8 | 555175960 | 0 | 0 | false |
| sys | netflow | index_id | oid | 73\7375 | 69396995 | 8 | 555175960 | 0 | 0 | true |
+--------+---------+-------------------------------+-----------+----------+----------+-----------+------------+------------+---------+--------+
Beware, the SQL statement CREATE INDEX is silently ignored in MonetDB.
It will create the necessary hash indices upon need.
Your first query triggers this.
I also notice that you don't seem to run in parallel.
Please, always indicate the platform used and MonetDB Version.
I have the following SQL generated from my Rails app, it is trying to get a list of all auto models that have live adverts in a marketplace app & from mysql:
SELECT `models`.* FROM `models`
INNER JOIN `autos` ON autos.model_id = models.id
INNER JOIN `ads` ON `ads`.id = `autos`.ad_id
WHERE (ads.ad_status_id = 4 AND pub_start_date < NOW() AND pub_end_date > NOW() AND models.manufacturer_id = 50 )
GROUP BY models.id ORDER BY models.name;
When I run an explain, this is what I get:
Id 1 1 1
Select Type SIMPLE SIMPLE SIMPLE
Table models autos ads
Type ref ref eq_ref
Possible Keys PRIMARY,manufacturer_id model_id,ad_id PRIMARY,quick_search,ad_status_id
Key manufacturer_id model_id PRIMARY
Key Length 5 4 4
Ref const concept_development.models.id concept_development.autos.ad_id
Rows 70 205 1
Extra Using where; Using temporary; Using filesort Using where Using where
I cannot understand why the query is generating a temporary table / using file-sort - all of the referenced keys are indexes. Been trying to figure this out for a few days now and getting nowhere.
Any help is very much appreciated!
EXPLAIN models:
+---------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(32) | YES | | NULL | |
| manufacturer_id | int(11) | YES | MUL | NULL | |
| vehicle_category_id | int(11) | NO | MUL | 1 | |
| synonym_names | longtext | YES | | NULL | |
+---------------------+-------------+------+-----+---------+----------------+
SHOW INDEXES FROM models:
+--------+------------+---------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------+------------+---------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+
| models | 0 | PRIMARY | 1 | id | A | 2261 | NULL | NULL | | BTREE | |
| models | 1 | manufacturer_id | 1 | manufacturer_id | A | 205 | NULL | NULL | YES | BTREE | |
| models | 1 | vehicle_category_id | 1 | vehicle_category_id | A | 7 | NULL | NULL | | BTREE | |
+--------+------------+---------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+
MODEL TABLE STATUS:
+--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+---------------------+-------------------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+---------------------+-------------------+----------+----------------+---------+
| models | MyISAM | 10 | Dynamic | 2261 | 26 | 61000 | 281474976710655 | 84992 | 0 | 2751 | 2010-09-28 18:42:45 | 2010-09-28 18:42:45 | 2010-09-28 18:44:00 | latin1_swedish_ci | NULL | | |
+--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+---------------------+-------------------+----------+----------------+---------+
EXPLAIN ADS
+------------------+--------------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------------------+------+-----+---------------------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| fp_urn | int(10) | NO | MUL | 0 | |
| user_id | int(10) | NO | MUL | 0 | |
| ad_status_id | int(3) unsigned | NO | MUL | 1 | |
| style_id | int(10) | NO | | 3 | |
| search_tags | varchar(255) | YES | | NULL | |
| title | varchar(255) | NO | | | |
| description | text | YES | | NULL | |
| currency | enum('EUR','GBP') | NO | | EUR | |
| price | decimal(8,2) | NO | MUL | 0.00 | |
| proposal_type | enum('Offered','Wanted') | NO | | Offered | |
| category_id | int(10) | YES | | 0 | |
| contact | varchar(50) | NO | MUL | | |
| area_id | int(10) | NO | | 0 | |
| origin_id | int(10) | NO | | 0 | |
| reject_reason_id | int(3) | NO | | 0 | |
| date_created | timestamp | NO | | 0000-00-00 00:00:00 | |
| last_modified | timestamp | NO | | CURRENT_TIMESTAMP | |
| pub_start_date | datetime | YES | | 0000-00-00 00:00:00 | |
| pub_end_date | datetime | YES | | 0000-00-00 00:00:00 | |
| bumped_up_date | datetime | YES | | 0000-00-00 00:00:00 | |
| state | smallint(6) | YES | | NULL | |
| eproofed | tinyint(1) | NO | | 0 | |
| is_featured | int(1) | NO | | 0 | |
| num_featured_imp | int(10) | YES | | 0 | |
| num_direct_imp | int(10) | YES | | 0 | |
| is_top_listed | int(1) | NO | | 0 | |
| delta | tinyint(1) | NO | | 0 | |
| ext_ref_id | varchar(50) | YES | | NULL | |
| email_seller | tinyint(1) | YES | | 1 | |
| sort_by | int(10) | YES | | 8 | |
| permalink | varchar(500) | YES | | NULL | |
| external_url | varchar(255) | YES | | NULL | |
+------------------+--------------------------+------+-----+---------------------+----------------+
SHOW TABLE STATUS FROM concept_development WHERE NAME LIKE 'ads';
+------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+
| ads | InnoDB | 10 | Compact | 656350 | 232 | 152748032 | 0 | 87736320 | 340787200 | 1148382 | 2010-09-29 09:55:46 | NULL | NULL | utf8_general_ci | NULL | checksum=1 delay_key_write=1 row_format=DYNAMIC | |
+------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+
SHOW INDEXES FROM ADS
+-------+------------+-----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+-----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
| ads | 0 | PRIMARY | 1 | id | A | 521391 | NULL | NULL | | BTREE | |
| ads | 1 | NewIndex1 | 1 | ad_status_id | A | 15 | NULL | NULL | | BTREE | |
| ads | 1 | NewIndex1 | 2 | pub_end_date | A | 260695 | NULL | NULL | YES | BTREE | |
| ads | 1 | NewIndex1 | 3 | category_id | A | 521391 | NULL | NULL | YES | BTREE | |
| ads | 1 | NewIndex1 | 4 | style_id | A | 521391 | NULL | NULL | | BTREE | |
| ads | 1 | NewIndex2 | 1 | user_id | A | 130347 | NULL | NULL | | BTREE | |
| ads | 1 | NewIndex3 | 1 | price | A | 7667 | NULL | NULL | | BTREE | |
| ads | 1 | contact | 1 | contact | A | 260695 | NULL | NULL | | BTREE | |
| ads | 1 | fp_urn | 1 | fp_urn | A | 521391 | NULL | NULL | | BTREE | |
+-------+------------+-----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
EXPLAIN autos
+-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+-------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+-------------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| ad_id | int(10) | YES | MUL | NULL | |
| style_id | int(10) | YES | MUL | NULL | |
| manufacturer_id | int(10) | NO | MUL | NULL | |
| model_id | int(10) | NO | MUL | NULL | |
| registration | varchar(10) | YES | | NULL | |
| year | int(4) | YES | | NULL | |
| fuel_type | enum('Petrol','Diesel') | NO | | Petrol | |
| colour | varchar(75) | YES | | NULL | |
| mileage | varchar(25) | NO | | Not Entered | |
| mileage_units | enum('mls','kms') | NO | | mls | |
| num_doors | varchar(25) | NO | | Not Entered | |
| num_owners | int(2) | YES | | NULL | |
| engine_size | varchar(10) | YES | | NULL | |
| transmission_type | enum('Manual','Automatic') | NO | | Manual | |
| body_type | enum('Saloon','Hatchback') | NO | | Saloon | |
| condition | varchar(75) | NO | | NA | |
| extra_features | text | YES | | NULL | |
| tax_expiry | varchar(7) | YES | | NULL | |
| nct_expiry | varchar(7) | YES | | NULL | |
| variation | text | YES | | NULL | |
| tax_class | enum('Agricultural','Bus') | NO | | Private | |
| co2 | int(9) | YES | | NULL | |
+-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+-------------+----------------+
SHOW TABLE STATUS FROM concept_development WHERE NAME LIKE 'autos'
+-------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+-------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+
| autos | InnoDB | 10 | Compact | 196168 | 136 | 26804224 | 0 | 26279936 | 340787200 | 485405 | 2010-09-17 22:09:45 | NULL | NULL | utf8_general_ci | NULL | checksum=1 delay_key_write=1 row_format=DYNAMIC | |
+-------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+-------------------------------------------------+---------+
show indexes from autos;
+-------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+
| autos | 0 | PRIMARY | 1 | id | A | 294937 | NULL | NULL | | BTREE | |
| autos | 1 | ad_id | 1 | ad_id | A | 294937 | NULL | NULL | YES | BTREE | |
| autos | 1 | style_id | 1 | style_id | A | 10 | NULL | NULL | YES | BTREE | |
| autos | 1 | manufacturer_id | 1 | manufacturer_id | A | 194 | NULL | NULL | | BTREE | |
| autos | 1 | model_id | 1 | model_id | A | 830 | NULL | NULL | | BTREE | |
+-------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+
From the MySQL documentation:
Temporary tables can be created under conditions such as these:
* If there is an ORDER BY clause and a different GROUP BY clause, or if the ORDER BY or GROUP BY contains columns from tables other than the first table in the join queue, a temporary table is created.
http://dev.mysql.com/doc/refman/5.1/en/internal-temporary-tables.html
Change all the text columns to varchar. If you need to maintain them as "text", you'll need to snowflake the schema and exclude the description tables from this query.
If any of the columns in any of the tables are text or blob, MySQL automatically creates an on-disk temporary table, rather than an in-memory temporary table. The temporary table itself isn't killing you, it's the fact that it's writing it to the disk.
http://dev.mysql.com/doc/refman/5.1/en/internal-temporary-tables.html
Some conditions prevent the use of an in-memory temporary table, in which case the server uses an on-disk table instead:
Presence of a BLOB or TEXT column in the table
You have a index on pub_end_date, but not on pub_start_date and your WHERE clause references both.
It looks like it is not using the pub_end_date index, but this could be because it needs to check pub_start_date as well.
This isn't explaining why but how about you rewrite your query to not use a group by? I think you're just joining on those tables to ensure there exists an ad of interest. So how about:
SELECT `models`.*
FROM `models`
WHERE models.manufacturer_id = 50
AND EXISTS ( SELECT * FROM `autos`
INNER JOIN `ads` ON `ads`.id = `autos`.ad_id
WHERE autos.model_id = models.id
AND ads.ad_status_id = 4
AND ads.pub_start_date < NOW()
AND ads.pub_end_date > NOW()
)
ORDER BY models.name;
The performance issues might be related to the group by, in which case this would improve performance.
Maybe it'd look a bit nicer with and NOW() between ads.pub_start_date and ads.pub_end_date, if you're allowed to do that in mysql (and if it works how you want with the edge cases).
I'm running the following query in MYSQL
select distinct straight_join
cu.entryid entryid,
t0.tokpos starting_position,
t3.tokpos ending_position,
t0.idxsent idxsent,
'TOKENS_44_340' tablename
from
TOKENS_44_340 t0,
constraints_appraisal cu,
TOKENS_44_340 t1,
TOKENS_44_340 t2,
TOKENS_44_340 t3
where
t0.token_surface = cu.token_0
and (cu.pos_0 is null OR t0.penntag like concat(cu.pos_0,'%'))
and t1.token_surface = cu.token_1
and (cu.pos_1 is null OR t1.penntag like concat(cu.pos_1,'%'))
and t2.token_surface = cu.token_2
and (cu.pos_2 is null OR t2.penntag like concat(cu.pos_2,'%'))
and t3.token_surface = cu.token_3
and (cu.pos_3 is null OR t3.penntag like concat(cu.pos_3,'%'))
and t0.tokpos = t1.tokpos - 1
and t1.tokpos = t2.tokpos - 1
and t2.tokpos = t3.tokpos - 1
and cu.token_4 is null
and cu.token_5 is null
and cu.token_6 is null
and cu.token_7 is null
and cu.token_8 is null
and cu.token_9 is null;
MySQL gives me the following query plan for this query:
+----+-------------+-------+------+---------------------------------------+------------------------+---------+------+------+-----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------------------------------+------------------------+---------+------+------+-----------------+
| 1 | SIMPLE | t0 | ALL | PRIMARY,TOKENS_44_340_index_44,tokpos | NULL | NULL | NULL | 49 | Using temporary |
| 1 | SIMPLE | cu | ALL | NULL | NULL | NULL | NULL | 7907 | Using where |
| 1 | SIMPLE | t1 | ref | PRIMARY,TOKENS_44_340_index_44,tokpos | TOKENS_44_340_index_44 | 399 | func | 4 | Using where |
| 1 | SIMPLE | t2 | ref | PRIMARY,TOKENS_44_340_index_44,tokpos | TOKENS_44_340_index_44 | 399 | func | 4 | Using where |
| 1 | SIMPLE | t3 | ref | TOKENS_44_340_index_44 | TOKENS_44_340_index_44 | 399 | func | 4 | Using where |
+----+-------------+-------+------+---------------------------------------+------------------------+---------+------+------+-----------------+
5 rows in set (0.00 sec)
As you can see, MySQL doesn't even so much as acknowledge the existance of my index token_0 on constraints_appraisal(token_0). Any idea why it's ignoring my index, and what I can do about it? I'm running mysql 5.0.51a-24+lenny4 for Debian stable.
P.S. I know I could make this query run faster by removing the straight_join constraint and let it use the token_surface index on t0, but it still wouldn't be as fast as it could be using the token_0 index on constraints_appraisal. I added the straight_join so that I could make my specific issue appear more clearly, and I plan remove it when I have the index working properly.
mysql> describe TOKENS_44_340;
+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| tokPos | int(11) | NO | PRI | NULL | |
| linePos | int(11) | YES | | NULL | |
| EOLs | int(11) | YES | | NULL | |
| idxsent | int(11) | YES | | NULL | |
| possent | int(11) | YES | | NULL | |
| brilltag | int(11) | YES | | NULL | |
| token_surface | varchar(132) | YES | MUL | NULL | |
| wordLen | int(11) | YES | | NULL | |
| capitalized | int(11) | YES | | NULL | |
| wordType | int(11) | YES | | NULL | |
| numDigit | int(11) | YES | | NULL | |
| numPunc | int(11) | YES | | NULL | |
| numAlpha | int(11) | YES | | NULL | |
| maxRep | int(11) | YES | | NULL | |
| pre1 | varchar(132) | YES | | NULL | |
| pre2 | varchar(132) | YES | | NULL | |
| pre3 | varchar(132) | YES | | NULL | |
| pre4 | varchar(132) | YES | | NULL | |
| suf1 | varchar(132) | YES | | NULL | |
| suf2 | varchar(132) | YES | | NULL | |
| suf3 | varchar(132) | YES | | NULL | |
| suf4 | varchar(132) | YES | | NULL | |
| dep_gov | int(11) | YES | MUL | NULL | |
| dep_rel | varchar(20) | YES | MUL | NULL | |
| penntag | varchar(30) | YES | | NULL | |
+---------------+--------------+------+-----+---------+-------+
25 rows in set (0.04 sec)
mysql> describe constraints_appraisal;
+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| entryid | int(11) | NO | PRI | 0 | |
| context | varchar(50) | YES | | NULL | |
| syntax | int(11) | YES | | NULL | |
| token_0 | varchar(50) | YES | MUL | NULL | |
| pos_0 | varchar(50) | YES | | NULL | |
| porter_0 | varchar(50) | YES | MUL | NULL | |
| token_1 | varchar(50) | YES | | NULL | |
| pos_1 | varchar(50) | YES | | NULL | |
| porter_1 | varchar(50) | YES | | NULL | |
| token_2 | varchar(50) | YES | | NULL | |
| pos_2 | varchar(50) | YES | | NULL | |
| porter_2 | varchar(50) | YES | | NULL | |
| token_3 | varchar(50) | YES | | NULL | |
| pos_3 | varchar(50) | YES | | NULL | |
| porter_3 | varchar(50) | YES | | NULL | |
| token_4 | varchar(50) | YES | | NULL | |
| pos_4 | varchar(50) | YES | | NULL | |
| porter_4 | varchar(50) | YES | | NULL | |
| token_5 | varchar(50) | YES | | NULL | |
| pos_5 | varchar(50) | YES | | NULL | |
| porter_5 | varchar(50) | YES | | NULL | |
| token_6 | varchar(50) | YES | | NULL | |
| pos_6 | varchar(50) | YES | | NULL | |
| porter_6 | varchar(50) | YES | | NULL | |
| token_7 | varchar(50) | YES | | NULL | |
| pos_7 | varchar(50) | YES | | NULL | |
| porter_7 | varchar(50) | YES | | NULL | |
| token_8 | varchar(50) | YES | | NULL | |
| pos_8 | varchar(50) | YES | | NULL | |
| porter_8 | varchar(50) | YES | | NULL | |
| token_9 | varchar(50) | YES | | NULL | |
| pos_9 | varchar(50) | YES | | NULL | |
| porter_9 | varchar(50) | YES | | NULL | |
| token_surface | varchar(200) | YES | | NULL | |
| fileid | varchar(100) | YES | | NULL | |
+---------------+--------------+------+-----+---------+-------+
35 rows in set (0.06 sec)
mysql> show index from constraints_appraisal;
+-----------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-----------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| constraints_appraisal | 0 | PRIMARY | 1 | entryid | A | 7907 | NULL | NULL | | BTREE | |
| constraints_appraisal | 1 | token_0 | 1 | token_0 | A | NULL | NULL | NULL | YES | BTREE | |
| constraints_appraisal | 1 | porter_0 | 1 | porter_0 | A | NULL | NULL | NULL | YES | BTREE | |
+-----------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
3 rows in set (0.05 sec)
It appears that the problem is that the two tables are in different character sets.
TOKENS_44_340 is in utf8
constraints_appraisal is in latin1