Select from a view taking a lot of time - view

I have created a view using below tables. This view takes a lot of time for returning output. I am using this view with other table by left join. Is there any way to increase the performance of below view?
DDL commands:
CREATE TABLE [dbo].[PRODUCT_TEXT]
(
[Product] [nvarchar](50) NOT NULL,
[Format] [nvarchar](3) NOT NULL,
[Text_Code] [nvarchar](8) NOT NULL,
[F_Date_Stamp] [datetime] NULL,
[DATA_CODE] [nvarchar](8) NULL,
CONSTRAINT [PK_Staging_T_PROD_TEXT] PRIMARY KEY CLUSTERED
(
[Product] ASC,
[Format] ASC,
[Text_Code] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].PHRASE_LINK]
(
[LINK_ID] [int] NOT NULL,
[PHRASE_ID] [int] NOT NULL,
[DATA_CODE] [nvarchar](8) NOT NULL,
[TEXT_CODE] [nvarchar](8) NOT NULL,
CONSTRAINT [PK_Staging_T_PHRASE_LINKAGE] PRIMARY KEY CLUSTERED
(
[LINK_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[PHRASE_TRANSLATIONS](
[F_TRANSLATION_ID] [int] NOT NULL,
[F_PHRASE_ID] [int] NOT NULL,
[F_LANGUAGE] [nvarchar](2) NOT NULL,
[F_PHRASE] [nvarchar](max) NULL,
CONSTRAINT [PK_PHRASE_TRANSLATIONS] PRIMARY KEY CLUSTERED
(
[F_TRANSLATION_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[PROD_ALIAS_TEXT]
(
[ALIAS] [varchar](50) NOT NULL,
[FORMAT] [varchar](3) NOT NULL,
[DATA_CODE] [varchar](8) NULL,
[TEXT_CODE] [varchar](8) NOT NULL,
CONSTRAINT [PROD_ALIAS_TEXT] PRIMARY KEY CLUSTERED
(
[F_ALIAS] ASC,
[F_FORMAT] ASC,
[F_TEXT_CODE] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]
GO
Non-clustered index details for these tables:
CREATE NONCLUSTERED INDEX IX_PROD_TEXT ON PRODUCT_TEXT (TEXT_CODE, PRODUCT)
CREATE NONCLUSTERED INDEX IX_PROD_TEXT_1 ON PROD_TEXT (DATA_CODE, PRODUCT)
CREATE NONCLUSTERED INDEX IX_PHRASE_LINK ON PHRASE_LINK (TEXT_CODE)
CREATE NONCLUSTERED INDEX IX_PHRASE_Translations ON PHRASE_transaltions(phrase_id)
CREATE NONCLUSTERED INDEX IX_PHRASE_Translations_LANG ON PHRASE_transaltions(LANGUAGE)
View definition:
CREATE VIEW [dbo].[MANU_TEST]
AS
SELECT
PT.PRODUCT AS PRODUCT, PT.TEXT_CODE AS TEXT_CODE,
PHT.PHRASE AS F_PHRASE
FROM
PRODUCT_TEXT PT
LEFT JOIN
PHRASE_LINK PHL ON PT.TEXT_CODE = PHL.TEXT_CODE
INNER JOIN
PHRASE_TRANSLATIONS PHT ON PHL.PHRASE_ID = PHT.PHRASE_ID
WHERE
PT.DATA_CODE = 'MANU' AND PHT.LANGUAGE = 'EN'
UNION ALL
SELECT
PT.F_ALIAS AS PRODUCT, PT.TEXT_CODE AS TEXT_CODE,
PHT.PHRASE AS F_PHRASE
FROM
PROD_ALIAS_TEXT PT
LEFT JOIN
PHRASE_LINK PHL ON PT.TEXT_CODE = PHL.TEXT_CODE
INNER JOIN
PHRASE_TRANSLATIONS PHT ON PHL.PHRASE_ID = PHT.PHRASE_ID
WHERE
PT.DATA_CODE = 'MANU' AND PHT.LANGUAGE = 'EN'
This view takes a lot of time to return output.

Related

Foreign key keeps saying not enough values

I have been trying to get this thing working for a few day now (foreign key) and it just don't work, and feel like every solution I used don't work, so i'm asking here to learn what was the problem and how to fix it
Table creation :
CREATE TABLE CUSTOMER
(
Customer_ID varchar(255) NOT NULL,
Customer_Name varchar(50),
Customer_Gender varchar(10),
Customer_DOB varchar(20) ,
CONSTRAINT CUSTOMER_PK PRIMARY KEY(Customer_ID)
) ;
CREATE TABLE PAYMENT
(
Payment_ID varchar(255) NOT NULL,
Cust_ID varchar(255),
Payment_Method varchar(30),
Payment_Date varchar(20),
Payment_Total NUMBER(10,2) ,
CONSTRAINT PAYMENT_PK PRIMARY KEY(Payment_ID),
CONSTRAINT fk_customer FOREIGN KEY(Cust_ID) REFERENCES CUSTOMER(Customer_ID)
) ;
Inserting values :
INSERT INTO CUSTOMER VALUES ('1277','Jenny','Female', ( TO_Date ( '03/04/1988' , 'DD/MM/yyyy')));
INSERT INTO CUSTOMER VALUES ('3423','Bryan','Male', ( TO_Date ( '15/06/1990' , 'DD/MM/YYYY')));
INSERT INTO CUSTOMER VALUES ('4385','Mohd Shafik','Male',( TO_Date ( '20/08/1993' , 'DD/MM/YYYY')));
INSERT INTO PAYMENT VALUES ('24P','Cash', ( TO_Date ( '11/02/2022' , 'DD/MM/YYYY')),24.50);
INSERT INTO PAYMENT VALUES ('09p','Online Transfer', ( TO_Date ( '08/04/2022' , 'DD/MM/YYYY')),25.00);
INSERT INTO PAYMENT VALUES ('10P','Cash', ( TO_Date ( '08/07/2022' , 'DD/MM/YYYY')),22.50);
The foreign keys are now working , but just for life of me can't figure out why the it spits out ORA-00947: not enough values and
ORA-01400: cannot insert NULL into ("SQL_GUUNNGDQAOXJVYPBKNMILVXJR"."PAYMENT"."PAYMENT_ID") ORA-06512: at "SYS.DBMS_SQL", line 1721
Also, Please explain to me how references works, i read a few places but they use words that just confuse me. Please and thank you!
Use DATE data types to store date values (and VARCHAR2 instead of VARCHAR) in your tables:
CREATE TABLE CUSTOMER(
Customer_ID varchar2(255) NOT NULL,
Customer_Name varchar2(50),
Customer_Gender varchar2(10),
Customer_DOB DATE,
CONSTRAINT CUSTOMER_PK PRIMARY KEY(Customer_ID)
);
CREATE TABLE PAYMENT(
Payment_ID varchar2(255) NOT NULL,
Cust_ID varchar2(255),
Payment_Method varchar2(30),
Payment_Date DATE,
Payment_Total NUMBER(10,2) ,
CONSTRAINT PAYMENT_PK PRIMARY KEY(Payment_ID),
CONSTRAINT fk_customer FOREIGN KEY(Cust_ID) REFERENCES CUSTOMER(Customer_ID)
);
Then name the columns in your INSERT statements:
INSERT INTO CUSTOMER (
customer_id, customer_name, customer_gender, customer_dob
) VALUES (
'1277','Jenny','Female', TO_Date('03/04/1988', 'DD/MM/yyyy')
);
INSERT INTO CUSTOMER (
customer_id, customer_name, customer_gender, customer_dob
) VALUES (
'3423','Bryan','Male', TO_Date('15/06/1990', 'DD/MM/YYYY')
);
INSERT INTO CUSTOMER (
customer_id, customer_name, customer_gender, customer_dob
) VALUES (
'4385','Mohd Shafik','Male', TO_Date('20/08/1993' , 'DD/MM/YYYY')
);
Then for the PAYMENT inserts, you have 5 columns in the table but only 4 pieces of data being inserted:
INSERT INTO PAYMENT (
payment_id, payment_method, payment_date, payment_total
) VALUES (
'24P','Cash', TO_Date('11/02/2022', 'DD/MM/YYYY'),24.50
);
INSERT INTO PAYMENT (
payment_id, payment_method, payment_date, payment_total
) VALUES (
'09p','Online Transfer', TO_Date( '08/04/2022' , 'DD/MM/YYYY'),25.00
);
INSERT INTO PAYMENT (
payment_id, payment_method, payment_date, payment_total
) VALUES (
'10P','Cash', TO_Date('08/07/2022', 'DD/MM/YYYY'),22.50
);
You have not provided a Cust_ID value so it will default to NULL in those rows.
If you want to provide a Cust_ID then add it to the statement:
INSERT INTO PAYMENT (
payment_id, cust_id, payment_method, payment_date, payment_total
) VALUES (
'ABC', '1277', 'Cash', TO_Date('08/07/2022', 'DD/MM/YYYY'),22.50
);
db<>fiddle here
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';
CREATE TABLE CUSTOMER
(
Customer_ID varchar(255) NOT NULL,
Customer_Name varchar(50),
Customer_Gender varchar(10),
Customer_DOB DATE ,
CONSTRAINT CUSTOMER_PK PRIMARY KEY(Customer_ID)
) ;
CREATE TABLE PAYMENT
(
Payment_ID varchar(255) NOT NULL,
Customer_ID varchar(255),
Payment_Method varchar(30),
Payment_Date DATE,
Payment_Total NUMBER(10,2) ,
CONSTRAINT PAYMENT_PK PRIMARY KEY(Payment_ID),
CONSTRAINT fk_customer FOREIGN KEY(Customer_ID) REFERENCES CUSTOMER(Customer_ID)
) ;
INSERT INTO CUSTOMER VALUES ('1277','Jenny','Female', ( TO_Date ( '03/04/1988' , 'DD/MM/yyyy')));
INSERT INTO PAYMENT VALUES ('24P','1277','Cash', ( TO_Date ( '11/02/2022' , 'DD/MM/YYYY')),24.50);
SELECT * FROM Customer;
CUSTOMER_ID CUSTOMER_NAME CUSTOMER_GENDER CUSTOMER_DOB
1277 Jenny Female 03-APR-1988
SELECT * FROM payment;
PAYMENT_ID CUSTOMER_ID PAYMENT_METHOD PAYMENT_DATE PAYMENT_TOTAL
24P 1277 Cash 11-FEB-2022 24.5

Alternative to window function in MariaDB 10.1

I have a windows function (over, partitioned by) in my code:
FROM (SELECT wp_posts.id,
wp_postmeta.post_id,
post_title,
post_type,
meta_value,
Row_number()
OVER(
partition BY post_title
ORDER BY wp_postmeta.meta_value) rn
but apparently this isn't supported on MariaDB before 10.2 (-I am using 10.1). Could someone please suggest alternative code which is both efficient and works on MariaDB 10.1 also?
dbfiddle provided, unfortunately with only MariaDB 10.2 as the oldest; can't test 10.1 directly here
create table wp_posts (
ID integer primary key auto_increment,
post_title varchar(30),
post_type varchar(30)
);
✓
create table wp_postmeta (
ID integer primary key auto_increment,
post_id integer,
meta_key varchar(30) not null default '_regular_price',
meta_value integer not null
);
✓
insert into wp_posts (post_title, post_type) values
('Apple Pie','Product'),
('French Toast','Product'),
('Shepards Pie','Product'),
('Jam Pie','Product'),
('Jam Pie','Product'),
('Plate','Not a Product'),
('Bucket','Not a Product'),
('Chequebook','Not a Product'),
('French Toast','Product'),
('French Toast','Product'),
('Banana','Product'),
('Banana','Product'),
('Banana','Product');
✓
insert into wp_postmeta (post_id, meta_value) values
(1,10),
(2,5),
(3,9),
(4,8),
(5,11),
(6,12),
(7,10),
(8,6),
(9,1),
(10,1),
(11,7),
(12,2),
(13,2);
✓
-- Deleting all duplicate products in wp_posts table
DELETE FROM wp_posts
WHERE id IN (SELECT id
FROM (SELECT id,
post_title,
post_type,
meta_value
FROM (SELECT wp_posts.id,
wp_postmeta.post_id,
post_title,
post_type,
meta_value,
Row_number()
OVER(
partition BY post_title
ORDER BY wp_postmeta.meta_value) rn
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
) t
WHERE t.rn <> 1) AS aliasx);
✓
db<>fiddle here

oracle syntax query

select line_number, times
FROM LINE
JOIN LINE_STOP ON LINE.ID=LINE_STOP.LINE_ID
JOIN PASSAGE ON LINE.STOP_ID = PASSAGE.LINE_STOP_ID
where times =
(select count(*)
from passage
where times between 500 and 620);
I am trying to calculate the number of time I have a number between 500 and 620 and sort it by line_number.
It gives me a table with the good column but no value.
these are my tables:
CREATE TABLE Line (
ID NUMBER(5,0) PRIMARY KEY,
Line_Number NUMBER(3,0) NOT NULL,
Direction VARCHAR2(5) NOT NULL,
Day_Week VARCHAR2(3) NOT NULL,
Stop_ID NUMBER(5,0) CONSTRAINT Line_Stop_FK REFERENCES Stop);
CREATE TABLE Line_Stop (
ID NUMBER(5,0) PRIMARY KEY,
Line_ID NUMBER(5,0) NOT NULL CONSTRAINT Line_Stop_Line_FK REFERENCES Line,
Stop_ID NUMBER(5,0) NOT NULL CONSTRAINT Line_Stop_Stop_FK REFERENCES Stop
);
CREATE TABLE Passage (
ID NUMBER(5,0) PRIMARY KEY,
Line_Stop_ID NUMBER(5,0) NOT NULL CONSTRAINT Passage_Line_Stop_FK REFERENCES Line_Stop,
Hours NUMBER(2,0) NOT NULL,
Minutes NUMBER(2,0) NOT NULL,
Times Number(4,0) NOT NULL
);
This filters the join having time between 500 and 620, and then groups it by line_number counting the number of occurences
select line_number, count(times)
FROM LINE
JOIN LINE_STOP ON (LINE.ID=LINE_STOP.LINE_ID AND LINE.STOP_ID=LINE_STOP.STOP_ID)
JOIN PASSAGE ON LINE.STOP_ID = PASSAGE.LINE_STOP_ID
where times between 500 and 620
group by line_number
order by line_number;

Create View inner join error

I'm creating a view and using inner join and receiving the following error:
ORA-00904: "B"."CUSTOMERNO": invalid identifier
This is the code I'm working with to create view and inner join
CREATE VIEW RentalInfoOct
(branch_no, branch_name, customer_no)
AS
SELECT b.branchNo, b.branchName, b.customerNo, c.customerNo
FROM branch b
INNER JOIN
customer c
ON b.customerNo = c.customerNo
Here are the create table commands as well.
CREATE TABLE Branch
(
branchNo SMALLINT NOT NULL,
branchName VARCHAR(20) NOT NULL,
branchAddress VARCHAR(40) NOT NULL,
PRIMARY KEY (BranchNo)
);
CREATE TABLE Customer
(
customerNo SMALLINT NOT NULL,
customerName VARCHAR(15) NOT NULL,
customerAddress VARCHAR(40) NOT NULL,
customerTel VARCHAR(10),
PRIMARY KEY (CustomerNo)
);
CREATE VIEW RentalInfoOct
(branch_no, branch_name, customer_no)
AS
SELECT b.branchNo, b.branchName, b.customerNo, c.customerNo
You specify 3 columns for the view, but you select 4 columns in SELECT

How do I create a partitioned table without initial partitions?

Say, I have the following DDL:
create table partition_test_table
(
id number(38,0) not null,
value varchar(255),
country_code varchar(2) not null,
creation_date date not null,
constraint pk_partition_test_table primary key (id)
)
partition by range ( creation_date )
subpartition by list ( country_code );
This doesn't work because it doesn't declare any initial partitions. How do I create a partitioned table without initial partitions? Is that even possible?
Thanks.

Resources