orientdb query linklist too slowly - orientdb

I have tow class: NOrder and NPassenger, relationship 1 : n, so NOrder has a filed named "passengers",whose type is linklist.
I want to select NOrder by NPassenger's field "passengerName",
I used sql
select from NOrder where 'John' in passengers.name
the NOrder ducuments count is 3 millon, this sql works too slowly, how can I do it?
thanks a lot

You can insert an index on the field name of the class NPassenger.
You can use this query
select expand(distinct(rid)) from (FIND REFERENCES (select from NPassenger where name = "John"))
Let me know.

Related

How to give alias for columns with same name after joining two tables in Postgres

Hi Guys I'm doing inner join for two tables and selecting all the columns from both tables. But I'm getting three cols with same name like id, created_at and updated_at.
Query:
SELECT addresses.* , facilities.* FROM facilities
INNER JOIN addresses
ON facilities.main_address_id = addresses.id
Is there any possible way that I can mention alias for above cols having same name while selecting all cols with * ?
Help of any kind would be appreciated! Thanks!
No you can't do this other than aliasing each column separately.
But if your query will be repetitive you could create VIEW:
CREATE OR REPLACE VIEW facilities_addresses AS
SELECT
addresses.column AS "addresses_column",
facilities.column AS "facilities_column"
FROM facilities
INNER JOIN addresses ON (facilities.main_address_id = addresses.id)
and then you can query:
SELECT * FROM facilities_addresses
yes you can
SELECT
addr.id as addressesId ,
addr.created_at as addresses_created_at,
addr.updated_at as addresses_update_at,
fac.id as facilitiesId,
fac.created_at as facilities_created_at,
fac.updated_at as facilities_updated_at FROM facilities as fac
INNER JOIN addresses as addr
ON facilities.main_address_id = addresses.id

Cast a PostgreSQL column to stored type

I am creating a viewer for PostgreSQL. My SQL needs to sort on the type that is normal for that column. Take for example:
Table:
CREATE TABLE contacts (id serial primary key, name varchar)
SQL:
SELECT id::text FROM contacts ORDER BY id;
Gives:
1
10
100
2
Ok, so I change the SQL to:
SELECT id::text FROM contacts ORDER BY id::regtype;
Which reults in:
1
2
10
100
Nice! But now I try:
SELECT name::text FROM contacts ORDER BY name::regtype;
Which results in:
invalid type name "my first string"
Google is no help. Any ideas? Thanks
Repeat: the error is not my problem. My problem is that I need to convert each column to text, but order by the normal type for that column.
regtype is a object identifier type and there is no reason to use it when you are not referring to system objects (types in this case).
You should cast the column to integer in the first query:
SELECT id::text
FROM contacts
ORDER BY id::integer;
You can use qualified column names in the order by clause. This will work with any sortable type of column.
SELECT id::text
FROM contacts
ORDER BY contacts.id;
So, I found two ways to accomplish this. The first is the solution #klin provided by querying the table and then constructing my own query based on the data. An untested psycopg2 example:
c = conn.cursor()
c.execute("SELECT * FROM contacts LIMIT 1")
select_sql = "SELECT "
for row in c.description:
if row.name == "my_sort_column":
if row.type_code == 23:
sort_by_sql = row.name + "::integer "
else:
sort_by_sql = row.name + "::text "
c.execute("SELECT * FROM contacts " + sort_by_sql)
A more elegant way would be like this:
SELECT id::text AS _id, name::text AS _name AS n FROM contacts ORDER BY id
This uses aliases so that ORDER BY still picks up the original data. The last option is more readable if nothing else.

OrientDB query too slow

OrientDB v2.1.1,
I have tow class: NOrder and NPassenger, relationship 1 : n, so NOrder has a filed named "passengers",whose type is linklist.
I have two fields (NOrder order_id, NPassenger. Name) index is established
NOrder ducuments count is 3 millon .
I explain these query:
1)select from NOrder where passengers contains(name = 'xxx')
why this query not involved index .
2)select from NOrder where 'xxx' in passengers.name
this query involved indexeses
this query cost 120sec .
thanks
I have tried with this structure
create class NPassenger
create property NPassenger.name String
create index NPassenger.name on NPassenger (name) NOTUNIQUE_HASH_INDEX
create class NOrder
create property NOrder.order_id String
create property NOrder.passengers linklist NPassenger
create index NOrder.order_id on NOrder (order_id) UNIQUE_HASH_INDEX
insert into NPassenger(name) values ("xxx") // 12:0
insert into NPassenger(name) values ("Alessandro") //12:1
insert into NOrder(order_id,passengers) values ("order 1",[12:0])
insert into NOrder(order_id,passengers) values ("order 2",[12:1])
Query 1
explain select from NOrder where passengers contains(name = 'xxx')
Query 2
explain select from NOrder where 'xxx' in passengers.name
None of the two queries use the index because the class target is NOrder.
UPDATE
Now I have 50002 NOrder and 50002 NPassenger.
If I execute the queries
explain select from NOrder where passengers contains(name = 'xxx')
and
explain select from NOrder where 'xxx' IN passengers.name'
I get
this is because no index on the name field is used (because the target class is the Norder class) and then the search is done on all 50002 records of the class Norder.
If I use the query
explain select from NPassenger where name = "xxx"
the index NPassenger.name is used because the target class is NPassenger

How to find primary key on a table in DB2?

I am unable to fetch primary key in DB2. I used following code but It is not working for me.
SELECT TBCREATOR, TBNAME, NAME, KEYSEQ
FROM SYSIBM.SYSCOLUMNS
WHERE TBCREATOR = 'DSN8710'
AND TBNAME = 'EMPLOYEE'
AND KEYSEQ > 0
ORDER BY KEYSEQ;
And what is the means of TBCREATOR in this code and how to modified TBCREATOR value according to my case?
I'll answer your last question first. creator is sometimes referred to as schema. If you're familiar with Oracle, this is roughly analogous to a database user (though not exactly).
As far as getting the "primary key" information, you probably want to know which index is the "clustering" index (which is what usually, but not always, determines the physical ordering of the rows on disk).
How you find the clustering index depends on the platform you're running:
Mainframe (z/OS):
SELECT
RTRIM(name) AS index_name
,RTRIM(creator) AS index_schema
,uniquerule
,clustering
FROM sysibm.sysindexes
WHERE tbname = #table
AND tbcreator = #schema
AND clustering = 'Y'
Then, to see the actual columns in that index, you perform this query:
SELECT colname AS name
FROM sysibm.sysindexes a
JOIN sysibm.syskeys b
ON a.name = b.ixname
AND a.tbcreator = b.ixcreator
WHERE a.name = #index_name
AND a.tbcreator = #index_schema
ORDER BY COLSEQ
Linux/Unix/Windows:
SELECT
RTRIM(indname) AS index_name
,RTRIM(indschema) AS index_schema
,uniquerule
,indextype
FROM syscat.indexes
WHERE tabname = #table
AND tabschema = #schema
AND indextype = 'CLUS'
Then, to see the actual columns in that index, you perform this query:
SELECT colnames as name
FROM sysibm.sysindexes
WHERE name = #index_name
AND tbcreator = #index_schema
ORDER BY NAME
LUW returns the list of columns as one string, delimited by +, which is kind of weird...

How do I traverse a relationship in a WHERE clause with OpenJPA?

I am new to JPA and OpennJPA. I have two entities UserDmo and SupplierDmo. Each Supplier can have several users and this relationship is established as follws,
In UserDmo,
Column(name="id_supplier")
private long idSupplier;
#ManyToOne(optional=true)
#JoinColumn(name="ID_SUPPLIER")
private SupplierDmo supplier;
In here column ID_SUPPLIER is the FK with referenced by ID column of the SupplierDmo. Using these two entities I tried to obtain result by following query.
SELECT u.id, u.modifiedDate FROM UserDmo u JOIN u.idSupplier s WHERE s.id = 1
But I got, Error message: Attempt to query field "s.id" from non-entity variable "s". Perhaps you forgot to prefix the path in question with an identification variable from your FROM clause?
I really appreciate your help on this
Try something like this :
SELECT u.id, u.modifiedDate FROM UserDmo u WHERE u.supplier.id = 1