SQL exercise: help needed The Next CEO of Stack OverflowHow can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to concatenate text from multiple rows into a single text string in SQL server?Inserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableSQL select only rows with max value on a columnHow to import an SQL file using the command line in MySQL?

Can someone explain this formula for calculating Manhattan distance?

What flight has the highest ratio of timezone difference to flight time?

Inexact numbers as keys in Association?

Can I use the word “Senior” as part of a job title directly in German?

Reference request: Grassmannian and Plucker coordinates in type B, C, D

What was Carter Burkes job for "the company" in "Aliens"?

Is there a way to save my career from absolute disaster?

How do I fit a non linear curve?

Is it okay to majorly distort historical facts while writing a fiction story?

What would be the main consequences for a country leaving the WTO?

(How) Could a medieval fantasy world survive a magic-induced "nuclear winter"?

AB diagonalizable then BA also diagonalizable

Help understanding this unsettling image of Titan, Epimetheus, and Saturn's rings?

Why the last AS PATH item always is `I` or `?`?

What difference does it make using sed with/without whitespaces?

Defamation due to breach of confidentiality

Is there a reasonable and studied concept of reduction between regular languages?

When "be it" is at the beginning of a sentence, what kind of structure do you call it?

Expressing the idea of having a very busy time

Is "three point ish" an acceptable use of ish?

Physiological effects of huge anime eyes

Can you teleport closer to a creature you are Frightened of?

Is it professional to write unrelated content in an almost-empty email?

What is the difference between "hamstring tendon" and "common hamstring tendon"?



SQL exercise: help needed



The Next CEO of Stack OverflowHow can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to concatenate text from multiple rows into a single text string in SQL server?Inserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableSQL select only rows with max value on a columnHow to import an SQL file using the command line in MySQL?










-2















Invent a test exercise so you can help me:



We have two tables that are the following:



table1:



id: auto_increment (primary key)
number_complete: varchar(255)


table2:



id: auto_increment (primary key)
name: varchar(255)
number_cut: varchar(255)


We have this data in each table:



table1:



enter image description here



table2:



enter image description here



well really in table 1 there are more than one million records and in the second table there are 34 thousand records.



the result I'm looking for is something like this:



enter image description here



What he was executing and continues to execute was this sentence:



select t2.name, count(t1.id) 
from table1 as t1, table2 as t2
where t1.number_complete like concat(t2.number_cut,'%')
group by t2.name;


But at this moment I've been waiting almost 1 hour for the sentence to be executed, as you may realize I am not an expert in SQL.










share|improve this question



















  • 5





    Thats a really horrible way of linking 2 tables

    – RiggsFolly
    Mar 7 at 18:05






  • 1





    @RiggsFolly But unfortunately not too uncommon.

    – Barmar
    Mar 7 at 18:06











  • I'm not arguing with you, for something I'm asking for help

    – Victor Nuñez
    Mar 7 at 18:07















-2















Invent a test exercise so you can help me:



We have two tables that are the following:



table1:



id: auto_increment (primary key)
number_complete: varchar(255)


table2:



id: auto_increment (primary key)
name: varchar(255)
number_cut: varchar(255)


We have this data in each table:



table1:



enter image description here



table2:



enter image description here



well really in table 1 there are more than one million records and in the second table there are 34 thousand records.



the result I'm looking for is something like this:



enter image description here



What he was executing and continues to execute was this sentence:



select t2.name, count(t1.id) 
from table1 as t1, table2 as t2
where t1.number_complete like concat(t2.number_cut,'%')
group by t2.name;


But at this moment I've been waiting almost 1 hour for the sentence to be executed, as you may realize I am not an expert in SQL.










share|improve this question



















  • 5





    Thats a really horrible way of linking 2 tables

    – RiggsFolly
    Mar 7 at 18:05






  • 1





    @RiggsFolly But unfortunately not too uncommon.

    – Barmar
    Mar 7 at 18:06











  • I'm not arguing with you, for something I'm asking for help

    – Victor Nuñez
    Mar 7 at 18:07













-2












-2








-2








Invent a test exercise so you can help me:



We have two tables that are the following:



table1:



id: auto_increment (primary key)
number_complete: varchar(255)


table2:



id: auto_increment (primary key)
name: varchar(255)
number_cut: varchar(255)


We have this data in each table:



table1:



enter image description here



table2:



enter image description here



well really in table 1 there are more than one million records and in the second table there are 34 thousand records.



the result I'm looking for is something like this:



enter image description here



What he was executing and continues to execute was this sentence:



select t2.name, count(t1.id) 
from table1 as t1, table2 as t2
where t1.number_complete like concat(t2.number_cut,'%')
group by t2.name;


But at this moment I've been waiting almost 1 hour for the sentence to be executed, as you may realize I am not an expert in SQL.










share|improve this question
















Invent a test exercise so you can help me:



We have two tables that are the following:



table1:



id: auto_increment (primary key)
number_complete: varchar(255)


table2:



id: auto_increment (primary key)
name: varchar(255)
number_cut: varchar(255)


We have this data in each table:



table1:



enter image description here



table2:



enter image description here



well really in table 1 there are more than one million records and in the second table there are 34 thousand records.



the result I'm looking for is something like this:



enter image description here



What he was executing and continues to execute was this sentence:



select t2.name, count(t1.id) 
from table1 as t1, table2 as t2
where t1.number_complete like concat(t2.number_cut,'%')
group by t2.name;


But at this moment I've been waiting almost 1 hour for the sentence to be executed, as you may realize I am not an expert in SQL.







mysql sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 18:18







Victor Nuñez

















asked Mar 7 at 18:02









Victor NuñezVictor Nuñez

226




226







  • 5





    Thats a really horrible way of linking 2 tables

    – RiggsFolly
    Mar 7 at 18:05






  • 1





    @RiggsFolly But unfortunately not too uncommon.

    – Barmar
    Mar 7 at 18:06











  • I'm not arguing with you, for something I'm asking for help

    – Victor Nuñez
    Mar 7 at 18:07












  • 5





    Thats a really horrible way of linking 2 tables

    – RiggsFolly
    Mar 7 at 18:05






  • 1





    @RiggsFolly But unfortunately not too uncommon.

    – Barmar
    Mar 7 at 18:06











  • I'm not arguing with you, for something I'm asking for help

    – Victor Nuñez
    Mar 7 at 18:07







5




5





Thats a really horrible way of linking 2 tables

– RiggsFolly
Mar 7 at 18:05





Thats a really horrible way of linking 2 tables

– RiggsFolly
Mar 7 at 18:05




1




1





@RiggsFolly But unfortunately not too uncommon.

– Barmar
Mar 7 at 18:06





@RiggsFolly But unfortunately not too uncommon.

– Barmar
Mar 7 at 18:06













I'm not arguing with you, for something I'm asking for help

– Victor Nuñez
Mar 7 at 18:07





I'm not arguing with you, for something I'm asking for help

– Victor Nuñez
Mar 7 at 18:07












3 Answers
3






active

oldest

votes


















0














Add an index on number_complete.



ALTER TABLE table1
ADD INDEX (number_complete);


Since indexes are B-trees by default, this makes matching prefixes reasonably efficient, and LIKE is able to take advantage of this.






share|improve this answer























  • Would the sentence of the post achieve that result or would it open to modify it?, I can not prove it, since the sentence is still running

    – Victor Nuñez
    Mar 7 at 18:13











  • I don't understand your comment. You need to cancel the query and start it again after making the change.

    – Barmar
    Mar 7 at 18:15











  • that if my query achieved the expected results that is my question

    – Victor Nuñez
    Mar 7 at 18:19











  • Yes, you don't need to change the query, just add the necessary indexes so it can be optimized.

    – Barmar
    Mar 7 at 18:21











  • I will try this. I'll see if it gives me results, faster.

    – Victor Nuñez
    Mar 7 at 18:23


















0














I would set the fields your are comparing to as indexes:



ALTER TABLE `table1` ADD INDEX(`number_complete`);
ALTER TABLE `table2` ADD INDEX(`number_cut`);


That should make the search run faster too.






share|improve this answer




















  • 2





    = does an exact match, be he's only trying to match the prefix.

    – Barmar
    Mar 7 at 18:15


















0














String operations are expensive. If they are done on the scale of millions, then they get extremely expensive and you will wait for hours. The solution is to avoid string operations in your filters.



You will need to ask yourself a question: how is table1 and table2 related to each-other? Are they many-to-one or many-to-many? If they are many-to-many, you can create a table3(table1_id, table2_id) table. If they are many-to-one, then you can create a table2_id for the table1 table. These will be numeric fields and easy to filter and search by.



You will need to write a script which will populate the new column(s). Yes, you will wait for an hour, or even more, but only once. Afterwards searches will be quick.






share|improve this answer























  • The horrible thing about all this is that the tables have no relation.

    – Victor Nuñez
    Mar 7 at 19:12











  • @VictorNuñez that's why I suggesting creating the relations. You can create nullable integer fields and mine the values with updates with equivalent where clauses that you have in the question.

    – Lajos Arpad
    Mar 7 at 19:43











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55050170%2fsql-exercise-help-needed%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Add an index on number_complete.



ALTER TABLE table1
ADD INDEX (number_complete);


Since indexes are B-trees by default, this makes matching prefixes reasonably efficient, and LIKE is able to take advantage of this.






share|improve this answer























  • Would the sentence of the post achieve that result or would it open to modify it?, I can not prove it, since the sentence is still running

    – Victor Nuñez
    Mar 7 at 18:13











  • I don't understand your comment. You need to cancel the query and start it again after making the change.

    – Barmar
    Mar 7 at 18:15











  • that if my query achieved the expected results that is my question

    – Victor Nuñez
    Mar 7 at 18:19











  • Yes, you don't need to change the query, just add the necessary indexes so it can be optimized.

    – Barmar
    Mar 7 at 18:21











  • I will try this. I'll see if it gives me results, faster.

    – Victor Nuñez
    Mar 7 at 18:23















0














Add an index on number_complete.



ALTER TABLE table1
ADD INDEX (number_complete);


Since indexes are B-trees by default, this makes matching prefixes reasonably efficient, and LIKE is able to take advantage of this.






share|improve this answer























  • Would the sentence of the post achieve that result or would it open to modify it?, I can not prove it, since the sentence is still running

    – Victor Nuñez
    Mar 7 at 18:13











  • I don't understand your comment. You need to cancel the query and start it again after making the change.

    – Barmar
    Mar 7 at 18:15











  • that if my query achieved the expected results that is my question

    – Victor Nuñez
    Mar 7 at 18:19











  • Yes, you don't need to change the query, just add the necessary indexes so it can be optimized.

    – Barmar
    Mar 7 at 18:21











  • I will try this. I'll see if it gives me results, faster.

    – Victor Nuñez
    Mar 7 at 18:23













0












0








0







Add an index on number_complete.



ALTER TABLE table1
ADD INDEX (number_complete);


Since indexes are B-trees by default, this makes matching prefixes reasonably efficient, and LIKE is able to take advantage of this.






share|improve this answer













Add an index on number_complete.



ALTER TABLE table1
ADD INDEX (number_complete);


Since indexes are B-trees by default, this makes matching prefixes reasonably efficient, and LIKE is able to take advantage of this.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 7 at 18:06









BarmarBarmar

434k36259362




434k36259362












  • Would the sentence of the post achieve that result or would it open to modify it?, I can not prove it, since the sentence is still running

    – Victor Nuñez
    Mar 7 at 18:13











  • I don't understand your comment. You need to cancel the query and start it again after making the change.

    – Barmar
    Mar 7 at 18:15











  • that if my query achieved the expected results that is my question

    – Victor Nuñez
    Mar 7 at 18:19











  • Yes, you don't need to change the query, just add the necessary indexes so it can be optimized.

    – Barmar
    Mar 7 at 18:21











  • I will try this. I'll see if it gives me results, faster.

    – Victor Nuñez
    Mar 7 at 18:23

















  • Would the sentence of the post achieve that result or would it open to modify it?, I can not prove it, since the sentence is still running

    – Victor Nuñez
    Mar 7 at 18:13











  • I don't understand your comment. You need to cancel the query and start it again after making the change.

    – Barmar
    Mar 7 at 18:15











  • that if my query achieved the expected results that is my question

    – Victor Nuñez
    Mar 7 at 18:19











  • Yes, you don't need to change the query, just add the necessary indexes so it can be optimized.

    – Barmar
    Mar 7 at 18:21











  • I will try this. I'll see if it gives me results, faster.

    – Victor Nuñez
    Mar 7 at 18:23
















Would the sentence of the post achieve that result or would it open to modify it?, I can not prove it, since the sentence is still running

– Victor Nuñez
Mar 7 at 18:13





Would the sentence of the post achieve that result or would it open to modify it?, I can not prove it, since the sentence is still running

– Victor Nuñez
Mar 7 at 18:13













I don't understand your comment. You need to cancel the query and start it again after making the change.

– Barmar
Mar 7 at 18:15





I don't understand your comment. You need to cancel the query and start it again after making the change.

– Barmar
Mar 7 at 18:15













that if my query achieved the expected results that is my question

– Victor Nuñez
Mar 7 at 18:19





that if my query achieved the expected results that is my question

– Victor Nuñez
Mar 7 at 18:19













Yes, you don't need to change the query, just add the necessary indexes so it can be optimized.

– Barmar
Mar 7 at 18:21





Yes, you don't need to change the query, just add the necessary indexes so it can be optimized.

– Barmar
Mar 7 at 18:21













I will try this. I'll see if it gives me results, faster.

– Victor Nuñez
Mar 7 at 18:23





I will try this. I'll see if it gives me results, faster.

– Victor Nuñez
Mar 7 at 18:23













0














I would set the fields your are comparing to as indexes:



ALTER TABLE `table1` ADD INDEX(`number_complete`);
ALTER TABLE `table2` ADD INDEX(`number_cut`);


That should make the search run faster too.






share|improve this answer




















  • 2





    = does an exact match, be he's only trying to match the prefix.

    – Barmar
    Mar 7 at 18:15















0














I would set the fields your are comparing to as indexes:



ALTER TABLE `table1` ADD INDEX(`number_complete`);
ALTER TABLE `table2` ADD INDEX(`number_cut`);


That should make the search run faster too.






share|improve this answer




















  • 2





    = does an exact match, be he's only trying to match the prefix.

    – Barmar
    Mar 7 at 18:15













0












0








0







I would set the fields your are comparing to as indexes:



ALTER TABLE `table1` ADD INDEX(`number_complete`);
ALTER TABLE `table2` ADD INDEX(`number_cut`);


That should make the search run faster too.






share|improve this answer















I would set the fields your are comparing to as indexes:



ALTER TABLE `table1` ADD INDEX(`number_complete`);
ALTER TABLE `table2` ADD INDEX(`number_cut`);


That should make the search run faster too.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 7 at 18:20

























answered Mar 7 at 18:14









jadelbejadelbe

355




355







  • 2





    = does an exact match, be he's only trying to match the prefix.

    – Barmar
    Mar 7 at 18:15












  • 2





    = does an exact match, be he's only trying to match the prefix.

    – Barmar
    Mar 7 at 18:15







2




2





= does an exact match, be he's only trying to match the prefix.

– Barmar
Mar 7 at 18:15





= does an exact match, be he's only trying to match the prefix.

– Barmar
Mar 7 at 18:15











0














String operations are expensive. If they are done on the scale of millions, then they get extremely expensive and you will wait for hours. The solution is to avoid string operations in your filters.



You will need to ask yourself a question: how is table1 and table2 related to each-other? Are they many-to-one or many-to-many? If they are many-to-many, you can create a table3(table1_id, table2_id) table. If they are many-to-one, then you can create a table2_id for the table1 table. These will be numeric fields and easy to filter and search by.



You will need to write a script which will populate the new column(s). Yes, you will wait for an hour, or even more, but only once. Afterwards searches will be quick.






share|improve this answer























  • The horrible thing about all this is that the tables have no relation.

    – Victor Nuñez
    Mar 7 at 19:12











  • @VictorNuñez that's why I suggesting creating the relations. You can create nullable integer fields and mine the values with updates with equivalent where clauses that you have in the question.

    – Lajos Arpad
    Mar 7 at 19:43















0














String operations are expensive. If they are done on the scale of millions, then they get extremely expensive and you will wait for hours. The solution is to avoid string operations in your filters.



You will need to ask yourself a question: how is table1 and table2 related to each-other? Are they many-to-one or many-to-many? If they are many-to-many, you can create a table3(table1_id, table2_id) table. If they are many-to-one, then you can create a table2_id for the table1 table. These will be numeric fields and easy to filter and search by.



You will need to write a script which will populate the new column(s). Yes, you will wait for an hour, or even more, but only once. Afterwards searches will be quick.






share|improve this answer























  • The horrible thing about all this is that the tables have no relation.

    – Victor Nuñez
    Mar 7 at 19:12











  • @VictorNuñez that's why I suggesting creating the relations. You can create nullable integer fields and mine the values with updates with equivalent where clauses that you have in the question.

    – Lajos Arpad
    Mar 7 at 19:43













0












0








0







String operations are expensive. If they are done on the scale of millions, then they get extremely expensive and you will wait for hours. The solution is to avoid string operations in your filters.



You will need to ask yourself a question: how is table1 and table2 related to each-other? Are they many-to-one or many-to-many? If they are many-to-many, you can create a table3(table1_id, table2_id) table. If they are many-to-one, then you can create a table2_id for the table1 table. These will be numeric fields and easy to filter and search by.



You will need to write a script which will populate the new column(s). Yes, you will wait for an hour, or even more, but only once. Afterwards searches will be quick.






share|improve this answer













String operations are expensive. If they are done on the scale of millions, then they get extremely expensive and you will wait for hours. The solution is to avoid string operations in your filters.



You will need to ask yourself a question: how is table1 and table2 related to each-other? Are they many-to-one or many-to-many? If they are many-to-many, you can create a table3(table1_id, table2_id) table. If they are many-to-one, then you can create a table2_id for the table1 table. These will be numeric fields and easy to filter and search by.



You will need to write a script which will populate the new column(s). Yes, you will wait for an hour, or even more, but only once. Afterwards searches will be quick.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 7 at 19:06









Lajos ArpadLajos Arpad

28.7k1862120




28.7k1862120












  • The horrible thing about all this is that the tables have no relation.

    – Victor Nuñez
    Mar 7 at 19:12











  • @VictorNuñez that's why I suggesting creating the relations. You can create nullable integer fields and mine the values with updates with equivalent where clauses that you have in the question.

    – Lajos Arpad
    Mar 7 at 19:43

















  • The horrible thing about all this is that the tables have no relation.

    – Victor Nuñez
    Mar 7 at 19:12











  • @VictorNuñez that's why I suggesting creating the relations. You can create nullable integer fields and mine the values with updates with equivalent where clauses that you have in the question.

    – Lajos Arpad
    Mar 7 at 19:43
















The horrible thing about all this is that the tables have no relation.

– Victor Nuñez
Mar 7 at 19:12





The horrible thing about all this is that the tables have no relation.

– Victor Nuñez
Mar 7 at 19:12













@VictorNuñez that's why I suggesting creating the relations. You can create nullable integer fields and mine the values with updates with equivalent where clauses that you have in the question.

– Lajos Arpad
Mar 7 at 19:43





@VictorNuñez that's why I suggesting creating the relations. You can create nullable integer fields and mine the values with updates with equivalent where clauses that you have in the question.

– Lajos Arpad
Mar 7 at 19:43

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55050170%2fsql-exercise-help-needed%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

Алба-Юлія

Захаров Федір Захарович