Storing MySQL Values ​​as Integers

I have two database tables that I use to create the next Twitter-style system.

sh_subscriptions
    => id
    => user_id
    => feed_id

sh_feeds
    => id
    => item
    => shop_name
    => feed_id

The problem with the preservation feed_idand not shop_namein sh_subscriptionslies in the fact that it requires a lot of compounds in the table below:

$id = $_POST['id'];
$user_id = $id['id'];
$shop_name = mysqli_escape_string($con, $_POST['shop_name']);

$query = "SELECT * FROM sh_subscriptions s INNER JOIN sh_feeds f ON s.feed_id = f.feed_id WHERE s.user_id = $user_id AND f.shop_name = '$shop_name'";
$result = mysqli_query($con, $query) or die(mysqli_error($con));

if (mysqli_num_rows($result) > 0)
{
    $query2 = "DELETE FROM sh_subscriptions s INNER JOIN sh_feeds f ON s.feed_id = f.feed_id WHERE s.user_id = $user_id AND f.shop_name = '$shop_name'";
    $result2 = mysqli_query($con, $query2) or die(mysqli_error($con));
}

else
{
    // insert the row instead
}

(I know there is an error somewhere in the if statement, but I will worry about this later.)

If I replaced feed_idwith shop_name, I could replace line 5 with this:

$query = "SELECT * FROM sh_subscriptions WHERE user_id = $user_id AND shop_name = '$shop_name'";

My question is: is it always better to store MySQL values ​​as integers, where possible, or in such a situation, would it be faster to have sh_subscriptionscontain shop_namerather than feed_id?

+4
source share
1

sh_subscriptions --, . .

: . , .

, sh_subscriptions. , .

, " ". , . MySQL , .

sh_subscriptions.

  • id. user_id feed_id . .
  • active... ... . 1, . , active 0.
  • subscribed_date, .
  • (active,user_id,feed_id) (active,feed_id,userId) . , .

:

   FROM sh_feed f
   JOIN sh_subscription s ON (f.feed_id = s.feed_id AND s.active = 1)
   JOIN sh_users u ON (s.user_id = u.user_id)
  WHERE f.shop_name = 'Joe the Plumber'

, , , , , , sh_subscriptions. .

. , , MySQL , sh_feeds, .

feed_id feed_id. user_id feed_id. , , , . . .

, . : () .

, .

. , , . .

+2

Source: https://habr.com/ru/post/1539279/


All Articles