What is the fastest way to browse a mysql table via internet? - c#

Let say I have a MySQL server with one table that has over 1 million records. For the sake of illustration let say those are products. To make it simple the table has only 3 columns:
product_id - name - price
Let say I have one table displaying the information. Through a TextBox, with each key press, I wan't to select the products that contains the string in that TextBox, additionally the TextBox can have multiple search strings (up to 3) divided by a character '^' so if the TextBox had a string like: "st ^ b ^ ry" it would select products that contains 'st', 'b' and 'ry'.
I made the query like this:
SELECT
product_id,
name,
price
FROM
products
WHERE
name LIKE %{Param1}%
AND name LIKE %{Param2}%
AND name LIKE %{Param3}%
ORDER BY
name ASC
Locally this is fine I guess but when the program is being used somewhere else the query and table takes forever to load after each key press. So...
What is the best way to do this?
Is there a better way to get what i want instead of 3 "LIKE" or is it
right how I did it?
UPDATE
I've checked the questions posted by Loathing and according to what I read the best solution is:
SELECT name FROM table
WHERE MATCH (name) AGAINST ('keyterm');
Having a FULLTEXT index (with MyISAM engine). But the problem with that is that it won't show partial entries. If I had a product called "1/2 Blue Strawberry" and in keyterm I have 'a' (as an example), nothing will show up. I don't want it to just look for words but every record containing whatever I want even if that is a single letter.

Related

c# textbox and mysql table interaction

I have developed a software with Visual Studio 2015-2017 and briefly speaking I have a textbox that send it's content as a parameter to a command on a MySql database to return the value in a DataGridView. It's a software to organize every
environmental complaint sent from a webpage.
My question is: Can I use that ONE textbox to search content for any row in a database table?
Like using the textbox to return every single environmental complaint with a persons name and using the same one to return every single environmental complaint with the complaint location.
It would be something like:
SELECT * FROM TABLE WHERE anything on a table = textbox parameter;
The answer is no, not in any normal sense the way you have shown, however you can build up a query to include the columns you want to search. I mean how are you (or sql) going to deal with non text fields and all sorts of other shenanigans
Example of what might get your started
SELECT ...
FROM yourtable
WHERE 'val' IN (field1, field2, field3, field4, ...)
or
WHERE field1 LIKE '%val%' or field2 LIKE '%val%' etc....
If you mean like separate the data in the same textbox. Then you could probably separate with spaces or lines using "\n" for new line or " " * x where x is an integer for spaces.
I tried this as an ideia and it aparrently works :
SELECT * FROM table WHERE Name = 'Person name' or complaintType = 'Person name'...
and so on with all the fields that i want, and "Person name" being the textbox parameter sent to the command...
thanks for the help!

Combining Entries in an ArrayList with all same Attributes Except for 1

I have two access database tables: one called trainers and one called trainerplan. I need to be able to query this database from my c# application in visual studio 2010 and display it. I cannot display the trainers multiple times; I have to display them once and then at the end, display all of the trainerplans they provide.
I can display all the trainers no problem. The problem I have is only display the trainer once with all of their trainer plans at the end. Right now, I am saving all it all into an arraylist.
Here is the query from the database that shows all of the trainers and their plans:
Here is how I need it to look in the application:
I have thought about creating a separate array for just the plans. I would have it check to see if the names are the same and if so, add those plans together. But I'm not sure if that's the right way to go nor really how to do that. I'm not even sure what to search for to get some ideas.
EDIT:
Here is the trainer table
Here is the trainer plan table
I assume that your key to link between those tables names TrainerID and the following SQL Query should work, then you don't have to separate the querying.
SELECT MAX(t.FirstName) AS FirstName,
MAX(t.LastName) AS LastName,
MAX(t.Phone) AS Phone,
MAX(t.[Hours]) AS [Hours],
( SELECT CONVERT(varchar(10), tp.PlanID) + ' '
FROM trainerplan tp
WHERE tp.TrainerID = t.ID
ORDER BY tp.PlanID
FOR XML PATH('') ) AS PlanID
FROM trainers t
GROUP BY t.TrainerID ;

How to add column of SUM (in data grid view) to sum data from releation of other data table?

Ok so I have two tables.
First one is "Service" which contains information about Car, date, employee working on car, and so on.
2nd table is "service journal". It contains this:
ID ID_Service Text Price
1 1 blabla 50
2 1 gfdgfd 75
Both tables have they own data grid view.
I want to add column "Sum" at first datagrid, which shows sum of all prices. For this example it would be 75.
How can I do that?
If "Service journal" means "price list", try to write this query:
SELECT ..., SUM([Price]) FROM xyz WHERE ID_Service=x OR ID_Service=y
And than use Eval in column settings and than you'll have generated new column whitch will be named as "Columnx" where x equals index of column in query.
You must use WHERE...OR... for selecting all things that have been done on car.
Or if "Service journal" means list of all repairs done on all cars whitch have ever been done in service, try to write this query:
SELECT ..., SUM([Price]) FROM Service_journal WHERE ID_Service=CarServiceNumber
This SQL query will get summary of prices of all services/repairs whitch have been done. This query is based on understanding of ID_Service in that it means id whitch is common for all repairs done on one car.
Bit unclear question. Hope this helps.

Which approach is better to retrieve data from a database

I am confused about selecting two approaches.
Scenario
there are two tables Table 1 and Table 2 respectively. Table 1 contains user's data for example first name, last name etc
Table 2 contains cars each user has with its description. i.e Color, Registration No etc
Now if I want to have all the information of all users then what approach is best to be completed in minimum time?
Approach 1.
Query for all rows in Table 1 and store them all in a list for ex.
then Loop through the list and query it and get data from Table 2 according to user saved in in first step.
Approach 2
Query for all rows and while saving that row get its all values from table 2 and save them too.
If I think of system processes then I think it might be the same because there are same no of records to be processed in both approaches.
If there is any other better idea please let me know
Your two approaches will have about the same performance (slow because of N+1 queries). It would be faster to do a single query like this:
select *
from T1
left join T2 on ...
order by T1.PrimaryKey
Your client app can them interpret the results and have all data in a single query. An alternative would be:
select *, 1 as Tag
from T1
union all
select *, 2 as Tag
from T2
order by T1.PrimaryKey, Tag
This is just pseudo code but you could make it work.
The union-all query will have surprisingly good performance because sql server will do a "merge union" which works like a merge-join. This pattern also works for multi-level parent-child relationships, although not as well.

SQL Query wildcard search

Im writing a C# web page thats tied to a listview. My client would like to be able to type in something or part of something that it would show results. For example he wants a textbox where he may put in a phone number, part of a phone number, a name, city or whatever and there would be a SP of sorts that finds then lists the info. How can I accomplish this in either a SQL Sp or within VS 2010?
SELECT cols
FROM tbl
WHERE field LIKE '%' + #input + '%'
As several others have suggested, use the LIKE operator.
However, do NOT just put the data the user typed in directly into your LIKE clause like others have suggested. This leads to a very simple and very dangerous vulnerability known as a SQL injection attack.
If you insert the user's input directly into
SELECT cols FROM tbl WHERE field LIKE '%' + input + '%'
then a user could put the following in the text box:
;DROP TABLE tbl; --
(as an example), which makes your SQL statement become:
SELECT cols FROM tbl WHERE field LIKE '%'; (the first part of your query)
DROP TABLE tbl; (the injected sql that you don't want to let people run; drop the database table)
-- '%' (the rest of your previous query is commented out)
Always make sure you used parametrised SQL statements, or at the minimum sanitize your inputs. You really don't want people to be able to run arbitrary SQL on your database server.
Jeff Atwood (of SO fame) has a short posting on this.
And it is worth reading this too :)
Most everyone has hit on part of the solution -- use the LIKE operator.
But I think another aspect of the problem can be addressed in SQL.
Create a computed varchar(MAX) column. Turn on a full text index on this field. Then all you need to do is do a sql like:
SELECT * from <TABLE_NAME> WHERE Keywords like '%<search term>%'
This way you don't have to do phone like <search> or name like <search> etc.
Use the LIKE operator.
SELECT * FROM Table WHERE PhoneNumber LIKE '%value%' OR Name LIKE '%value%' OR
City LIKE '%value%'
If you want to use one textbox which could contain many different kinds of data, you need to be specific in your code about which database tables and columns you will search, and in what order.
For example, you might write a query that does this:
First, search in the Customer table
in the FirstName and LastName columns
for a name LIKE the one in the
textbox. SELECT the CustomerID for
all of the matches.
Next, search in both the Customer
table and the Supplier table, in the
PhoneNumber column, for a phone
number LIKE the one in the textbox.
SELECT the CustomerID or SupplierID
for all of the matches. If any results are found, combine them with the results of the first query.
Continue searching for street
addresses, and querying other tables.
Add new records to the resultset as
you go along.
After you have queried all of the tables that you want to search in, you will have a resultset containing ID's. You need to do another series of SELECTs to get the information you want to display to the user. If you mix customers and suppliers (and employees, etc), this could become quite complicated.
As you can see from this, it would be much easier to have separate textboxes for each search criteria. One textbox for first name, another for last name, a third for company name. A separate textbox for phone number. And if you are mixing data for customers, suppliers, employees, etc, you should have the user indicate (perhaps on a dropdown list or with checkboxes) which types of people to search, so you know which tables to query.

Categories