I was trying to execute sql:
select last(*) from mytable where index=index-1
Describe mytable:
Field | Type | Length | Note |
=================================================================================
ts | TIMESTAMP | 8 | |
gauge | DOUBLE | 8 | |
index | NCHAR | 11 | TAG |
The result occur error:
Invalid operation
index is nchar type so you should change the SQL to
select last(*) from mytable where index='some_value';
Related
I am trying to change the type of one of the columns on my table from one that takes arrays filled with strings to one that takes JSON.
The SQL I'm trying to execute looks like:
ALTER TABLE my_table
ALTER COLUMN my_column TYPE JSON USING my_column::json
But I get an error back saying "cannot cast type character varying[] to json".
The column I'm trying to change is empty, there are no rows so there is no data that needs to be cast to JSON. Since it's empty I've thought of dropping the column and remaking it but I'd like to keep the column and just change its type if possible. I'm not a whizz with PostgreSQL so any nudge in the right direction would be appreciated.
array_test
Table "public.array_test"
Column | Type | Collation | Nullable | Default
---------------+---------------------+-----------+----------+---------
id | integer | | |
array_fld | integer[] | | |
numeric_array | numeric[] | | |
jsonb_array | jsonb[] | | |
varchar_array | character varying[] | | |
text_array | text[] | | |
ALTER TABLE array_test
ALTER COLUMN varchar_array TYPE json
USING array_to_json(varchar_array);
\d array_test
Table "public.array_test"
Column | Type | Collation | Nullable | Default
---------------+-----------+-----------+----------+---------
id | integer | | |
array_fld | integer[] | | |
numeric_array | numeric[] | | |
jsonb_array | jsonb[] | | |
varchar_array | json | | |
text_array | text[] | | |
I recently found the select syntax in TDengine SQL is slightly different from mysql due to stable. But I cannot give a reasonable explanation. Let me show the emample:
Here is the stable structure:
describe mystable;
Field | Type | Length | Note |
=================================================================================
ts | TIMESTAMP | 8 | |
value | DOUBLE | 8 | |
tag1 | NCHAR | 16 | TAG |
tag2 | NCHAR | 43 | TAG |
tag3 | NCHAR | 29 | TAG |
tag4 | NCHAR | 10 | TAG |
tag5 | NCHAR | 2 | TAG |
When I execute:
select count(*) from mystable
count(*) |
========================
270419 |
And count on a colume, the result should be the same:
select count(value) from mystable
count(value) |
========================
270419 |
However, When count on a tag, the results is different:
select count(tag1) from mystable
count(tag1) |
========================
13 |
So what does select(tag_name) actually means in TDengine sql?
After get the response of professional personnel of R&D. The select(tag_name) means number of tables belong to this stable with specified tag, we can print it out by using sql:
select tbname, tag1 from stable;
From my understanding, select count(*) or count(column) returns the number of records of each child table which belongs to the same super table. But tags are different, although tags can be used to filter records for each child table, but does not attach to each record, so select count(tag) returns number of distinct tags for stable, but not number of total records.
There is table named history in Zabbix database, I have created partitions on this table.
And the partition type is range and column type is UNIX_TYPESTAMP.
After the date is changed zabbix service does not insert data to the related partition.
What is the problem?
And how do I display all partitions?
Could you please help how do I write data to the related partitions?
Sample of Partition creation statement;
.
.
.
ALTER TABLE zabbix.history_test PARTITION BY RANGE(clock)(PARTITION
p28082021 VALUES LESS THAN(UNIX_TIMESTAMP("2021-08-28 00:00:00"
))ENGINE=InnoDB);
Server version: 10.1.31-MariaDB MariaDB Server
EXPLAIN PARTITIONS SELECT * FROM zabbix.history;
+------+-------------+---------+------------+------+---------------+------
| id | select_type | table | partitions | type | possible_keys | key |
key_len | ref | rows | Extra |
| 1 | SIMPLE | history | p28082021 | ALL | NULL | NULL
| NULL | NULL | 18956757 | |
SELECT DISTINCT PARTITION_EXPRESSION FROM
INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME='history' AND
TABLE_SCHEMA='zabbix';
+----------------------+
| PARTITION_EXPRESSION |
+----------------------+
| clock |
+----------------------+
MariaDB [(none)]> SELECT PARTITION_ORDINAL_POSITION, TABLE_ROWS, PARTITION_METHOD
FROM information_schema.PARTITIONS
WHERE TABLE_SCHEMA = 'zabbix' AND TABLE_NAME = 'history';
+----------------------------+------------+------------------+
| PARTITION_ORDINAL_POSITION | TABLE_ROWS | PARTITION_METHOD |
+----------------------------+------------+------------------+
| 1 | 18851132 | RANGE |
+----------------------------+------------+------------------+
SELECT FROM_UNIXTIME(MAX(clock)) FROM zabbix.history;
+---------------------------+
| FROM_UNIXTIME(MAX(clock)) |
+---------------------------+
| 2018-04-07 23:06:06 |
+---------------------------+
SELECT FROM_UNIXTIME(MIN(clock)) FROM zabbix.history;
+---------------------------+
| FROM_UNIXTIME(MIN(clock)) |
+---------------------------+
| 2018-04-06 01:06:23 |
+---------------------------+
This document help me to create partition on clock column.
There are stored procedures, that create partitions,you can check it.
https://www.zabbix.org/wiki/Docs/howto/mysql_partition
I use PostgreSQL 10.1 and:
CREATE TABLE human
(
id ... NOT NULL,
gender ...,
height ...,
weight ...,
eye ...,
hair ...,
...
);
I have an input form through which I insert the data. I wish an elegant and proper way by which I can SELECT which columns required to be DISPLAYED in that form, something like weight ... DISPLAYED, or eye ... NOT DISPLAYED, .
One way is to correspond NULL with DISPLAYED (when NOT NULL then display it, or when NULL then do not display it) and use information_schema which (corresponding) makes me no so happy:
Another way is to:
CREATE TABLE human_column
(
id ... NOT NULL,
characteristic character varying(...),
is_displayed boolean
);
where characteristic data are the names of the columns of human table.
Is there a better way to add a direct foreign attribute to the columns of a table? (In 51.7. pg_attribute there is a column named attoptions. Would it be used?)
specifying "options" for columns to define if they will be "displayed" or not seems a little overhead. Imagine you keep such list in human_column. To modify it you would need to update it with new is_displayed values. Then you would need to build column list to be selected in query.
When you create a view, you do the same (specify a list of columns to be displayed) and then you can just query the view, without need to dynamically build the query. Also you can always check the current list of included columns from catalog or information_schema.
The only "not cosy" feature of a view - you can't change columns in it, thus you have to drop and create it again.
drop/create view on demand looks cheaper to me then dynamically building query with list of columns on each select still.
demo:
db=# create view v as select oid,datname from pg_database;
CREATE VIEW
db=# select * from v;
oid | datname
-------+-----------
13505 | postgres
16384 | t
1 | template1
13504 | template0
16419 | o
(5 rows)
checking list of columns:
db=# select column_name,ordinal_position,column_default,is_nullable,data_type,character_maximum_length from information_schema.columns where table_name = 'v';
column_name | ordinal_position | column_default | is_nullable | data_type | character_maximum_length
-------------+------------------+----------------+-------------+-----------+--------------------------
oid | 1 | | YES | oid |
datname | 2 | | YES | name |
(2 rows)
same for original table:
db=# select column_name,ordinal_position,column_default,is_nullable,data_type,character_maximum_length from information_schema.columns where table_name = 'pg_database';
column_name | ordinal_position | column_default | is_nullable | data_type | character_maximum_length
---------------+------------------+----------------+-------------+-----------+--------------------------
datname | 1 | | NO | name |
datdba | 2 | | NO | oid |
encoding | 3 | | NO | integer |
datcollate | 4 | | NO | name |
datctype | 5 | | NO | name |
datistemplate | 6 | | NO | boolean |
datallowconn | 7 | | NO | boolean |
datconnlimit | 8 | | NO | integer |
datlastsysoid | 9 | | NO | oid |
datfrozenxid | 10 | | NO | xid |
datminmxid | 11 | | NO | xid |
dattablespace | 12 | | NO | oid |
datacl | 13 | | YES | ARRAY |
(13 rows)
I'm using a query looking like this: SELECT plan_table_output FROM table(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'BASIC'));
The output it gives me looks like this:
EXPLAINED SQL STATEMENT:
------------------------
SELECT /*+ PARALLEL(T1 8 ) */ * FROM T1
Plan hash value: 2494645258
-----------------------------------------
| Id | Operation | Name |
-----------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | PX COORDINATOR | |
| 2 | PX SEND QC (RANDOM)| :TQ10000 |
| 3 | PX BLOCK ITERATOR | |
| 4 | TABLE ACCESS FULL| T1 |
-----------------------------------------
But I want the output like this:
-----------------------------------------
| Id | Operation | Name |
-----------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | PX COORDINATOR | |
| 2 | PX SEND QC (RANDOM)| :TQ10000 |
| 3 | PX BLOCK ITERATOR | |
| 4 | TABLE ACCESS FULL| T1 |
-----------------------------------------
Without the unnecessary (for me at least) EXPLAINED SQL STATEMENT and Plan hash value.#
Is there any way to do this, without using sed or awk to remove the unnecessary lines?
If you really want to have a nice graphical representation of the execution plan, then use the EXPLAIN PLAN window in Oracle SQL Developer.
EDIT
Based on #a_horse_with_no_name solution, tweaking a bit more gives the exact desired output.
SQL> SELECT plan_table_output
2 FROM table(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'BASIC'))
3 WHERE substr(plan_table_output,1,1) in ('|', '-')
4 /
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
------------------------
----------------------------------
| Id | Operation | Name |
----------------------------------
| 0 | SELECT STATEMENT | |
| 1 | TABLE ACCESS FULL| EMP |
----------------------------------
7 rows selected.
So, I still see the PLAN_TABLE_OUTPUT in the output. Setting pagesize to 0 should take of it.
SQL> set pagesize 0
SQL> SELECT plan_table_output
2 FROM table(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'BASIC'))
3 WHERE substr(plan_table_output,1,1) in ('|', '-')
4 /
------------------------
------------------------------------------------------------
| Id | Operation | Name |
------------------------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | COLLECTION ITERATOR PICKLER FETCH| DISPLAY_CURSOR |
------------------------------------------------------------
7 rows selected.
Now there is still something unwanted, ------------------------, let's just not include in the output at all.
SQL> SELECT plan_table_output
2 FROM table(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'BASIC'))
3 WHERE substr(plan_table_output,1,1) in ('|', '-')
4 and plan_table_output not in('------------------------')
5 /
------------------------------------------------------------
| Id | Operation | Name |
------------------------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | COLLECTION ITERATOR PICKLER FETCH| DISPLAY_CURSOR |
------------------------------------------------------------
6 rows selected.
SQL>
You could add a where condition:
SELECT plan_table_output
FROM table(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'BASIC'))
WHERE substr(plan_table_output,1,1) in ('|', '-')
This will however not remove some of the "underlines" in the summary section of the output. However you can extend the where clause to include more rules